1.file_get_contents(path,include_path,context,start,max_length)
path 必需。規定要讀取的文件。
include_path 可選。如果也想在 include_path 中搜尋文件的話,可以將該參數設為 “1”。
context 可選。規定文件句柄的環境。context 是一套可以修改流的行為的選項。若使用 null,則忽略。
start 可選。規定在文件中開始讀取的位置。該參數是 PHP 5.1 新加的。
max_length 可選。規定讀取的字節數。該參數是 PHP 5.1 新加的。 1,此函數可以用來打開一個網絡地址 可以實現簡單的網頁抓取 2.此函數可以讀取本地的文件 3.此函數可以模擬post請求
網頁抓取
一般用file_get_contents或者fopen, file , readfile等函數讀取url的時候 會創建一個$http_response_header變量保存HTTP響應的報頭,使用fopen等函數打開的數據流信息可以用stream_get_meta_data獲取
file_put_contents('./1.txt', var_export($_POST, true));//把接收的數據儲存起來 $html = file_get_contents('http://www.baidu.com'); print_r($http_response_header); $f = fopen('http://www.baidu.com', 'o'); print_r(stream_get_meta_data($f)); fclose($f);
post請求
$url = 'www.baidu.com'; $data = [ 'appkey'=> '1111', 'text' => '2222', ]; $data = http_build_query($data); $opts = [ 'http' => [ 'method' => 'POST', 'header' => "Content-type:application/x-www-form-urlencoded\r\n". "Content-Length: ".strlen($data)."\r\n". "Cookie: PHPSESSID=13ROTEGFGJDFDFDOGDFGD"."\r\n". "User-Agent: Mozilla/5.0(Windows: U; Windows NT 6.1; zh-CH; rv: 1.9.2.13) Gecko/20101203 Firefox/3.6.13"."\r\n". "Referer:http://aiyooyoo.com/index.php/archives/7/"."\r\n", 'content' => $data, ], ]; $context = stream_context_create($opts); $html = @file_get_contents($url, false, $context);