3

The function I am using is:

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

And the call is:

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

The original form is located on the following page, but I'm using the form's action page in the call:

wunderground.com/history/

Ultimately I want to get contents from the redirected page, which is for example:

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

However as above, the form takes different elements, i.e. code, month, day, year.

Lawrence DeSouza
  • 984
  • 5
  • 16
  • 34
  • Try curl: [Make curl follow redirects?](http://stackoverflow.com/questions/3519939/make-curl-follow-redirects) – Antony Jan 25 '13 at 07:46
  • This should work if I am not mistaken as [this](http://us3.php.net/manual/en/context.http.php#context.http.follow-location) says the default value is to follow redirects. Unless `max_redirects` is being reached or it is timing out. – kittycat Jan 25 '13 at 08:09

2 Answers2

4

Why not 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,
    );

 }

Also, your function is named post but you are doing GET request

Timur
  • 6,668
  • 1
  • 28
  • 37
-2

try below function

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

 }
Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
  • Huh? All you did as far as I can see is change the boolean value for the 2nd argument to `true` if you read the [documentation](http://us3.php.net/manual/en/function.file-get-contents.php#refsect1-function.file-get-contents-parameters) that just toggles searching in the include path or not. – kittycat Jan 25 '13 at 08:07
  • thanks @crypticツ . yes only change in boolean part. I have executed the code and it is working fine. weather api is returning the perfect result. – Ripa Saha Jan 25 '13 at 08:11