Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php get url重定向时,文件\u get\u contents()不工作_Php_File Get Contents - Fatal编程技术网

Php get url重定向时,文件\u get\u contents()不工作

Php get url重定向时,文件\u get\u contents()不工作,php,file-get-contents,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 (

我使用的功能是:

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
但是,如上所述,表单采用不同的元素,即代码、月、日、年

试试下面的功能

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
);

 }
为什么不呢


此外,您的函数名为
post
,但您正在执行
GET
request

Try curl:如果我没有弄错,这应该可以工作,因为默认值是遵循重定向。除非已到达max_重定向,或者正在超时。嗯?据我所知,您所做的只是将第二个参数的布尔值更改为
true
,如果您阅读了只需切换包含路径中的搜索与否。谢谢@crypticツ . 是,仅更改布尔值部分。我已经执行了代码,它工作得很好。天气api正在返回完美的结果。我正在做一些调整,但它看起来会工作,谢谢!
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,
    );

 }