使用file_get_content

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 新加的。
<?php
$hdrs = array(
  'http' =>array(
      'header' => 
           "Accept: application/json\r\n" .
           "Accept-Encoding: gzip, deflate\r\n" .
           "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n" .
           "Connection: keep-alive\r\n" .
           "Cookie: mzys_is_frist_access=1; PHPSESSID=4kacoqfe4ae33km3h0l2imjfd6\r\n" .
           "Host: http://1212.ip138.com/ic.asp\r\n" .
           "Referer: http://1212.ip138.com/ic.asp\r\n" .
           "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0\r\n" .
           "X-Requested-With: XMLHttpRequest",
           'timeout'=>2
        ),
    'content' => http_build_query($post, '', '&'),
);
$context = stream_context_create($hdrs);
//方式1
echo file_get_contents('http://1212.ip138.com/ic.asp', 0, $context);
print_r($http_response_header); // 顯示返回的頭信息  
//方式2
// $fp = fopen("http://1212.ip138.com/ic.asp", 'r', false, $context);
// fpassthru($fp);
// fclose($fp);

失敗重試

$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE) $cnt++;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章