当get url重定向时,File_get_contents()不起作用

时间:2013-01-25 07:22:25

标签: php file-get-contents

我正在使用的功能是:

function http_post ($url, $data)
{
$data_url = http_build_query ($data);
$data_len = strlen ($data_url);
date_default_timezone_set('America/New_York');

return array ('content'=>file_get_contents ($url, false
    , stream_context_create (array ('http'=>array (
    'method'=>'GET', 
    'header'=>"Connection: close\r\nContent-Length: $data_len\r\nContent-type: application/x-www-form-urlencoded\r\n", 
    'content'=>$data_url
    )))), 
    'headers'=>$http_response_header
    );
}

电话是:

http_post('http://www.wunderground.com/cgi-bin/findweather/getForecast/', array('airportorwmo'=>'query','historytype'=>'DailyHistory','backurl'=>"/history/index.html",'code'=>"$myCode",'month'=>"$myMonth",'day'=>"$myDay",'year'=>"$myYear"));

原始表单位于以下页面,但我在通话中使用表单的操作页面:

wunderground.com/history/

最终我想从重定向页面获取内容,例如:

http://www.wunderground.com/history/airport/CWTA/2013/1/24/DailyHistory.html?req_city=McTavish&req_state=QC&req_statename=Quebec&MR=1

然而,如上所述,表单采用不同的元素,即代码,月,日,年。

2 个答案:

答案 0 :(得分:3)

为什么不cURL

function http_post ($url, $data)
{
    $data_url = http_build_query ($data);
    $data_len = strlen ($data_url);
    date_default_timezone_set('America/New_York');
    $curl = curl_init($url);
    curl_setopt_array(array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
    ));

    $content = curl_exec();

    curl_close($curl);

    return array (
        'content' => $content,
        'headers' => $http_response_header,
    );

 }

此外,您的功能名为post,但您正在执行GET请求

答案 1 :(得分:-2)

尝试以下功能

function http_post ($url, $data)
{
    $data_url = http_build_query ($data);
    $data_len = strlen ($data_url);
   date_default_timezone_set('America/New_York');

   return array ('content'=>file_get_contents ($url, true
, stream_context_create (array ('http'=>array (
'method'=>'GET', 
'header'=>"Connection: close\r\nContent-Length: $data_len\r\nContent-type: application/x-www-form-urlencoded\r\n", 
'content'=>$data_url
)))), 
'headers'=>$http_response_header
);

 }
相关问题