2

I followed this post to see how to pass data to php's file_get_contents function but I can't seem to get the data back using:

$data = $_POST['my_param']; // Where my_param is the name of the parameter passed

Does anybody know how to get back the value of the parameter sent to file_get_contents in the script that I'm calling with file_get_contents?

Thanks in advance

Community
  • 1
  • 1
user765368
  • 19,590
  • 27
  • 96
  • 167
  • @dandavis Unless you put `method = "POST"` in the context object. – Barmar Sep 09 '14 at 21:10
  • Your question isn't clear. Are you asking how to access the parameters in the script you're calling with `file_get_contents`, or in the script that contains `file_get_contents`? – Barmar Sep 09 '14 at 21:12
  • @dandavis I did put POST, but still didn't work – user765368 Sep 09 '14 at 21:12
  • @Barmar In the script that I'm calling with file_get_contents – user765368 Sep 09 '14 at 21:12
  • Then `$_POST['my_param']` should work. Please post the caller script. – Barmar Sep 09 '14 at 21:13
  • Can you please post the code where you actually call `file_get_contents()`? – lxg Sep 09 '14 at 21:13
  • @Barmar: that's news to me, thanks! – dandavis Sep 09 '14 at 21:13
  • @dandavis It shouldn't be, it's explained in the question he linked to with "I followed this post". – Barmar Sep 09 '14 at 21:14
  • @lxg, I used the exact same code of the answer that was accepted in that post http://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents/2445332#2445332 – user765368 Sep 09 '14 at 21:15
  • @user765368: Then your data is in either the `var1` or `var2` variables. ;) Seriously, please post your exact code, there seems to be some sort of typo, which may have gotten there when adapting the code. – lxg Sep 09 '14 at 21:16

1 Answers1

3

There is example @ php.net http://php.net/manual/en/function.file-get-contents.php#102575

code:

<?php
/**
make an http POST request and return the response content and headers
@param string $url    url of the requested script
@param array $data    hash array of request variables
@return returns a hash array with response content and headers in the following form:
    array ('content'=>'<html></html>'
        , 'headers'=>array ('HTTP/1.1 200 OK', 'Connection: close', ...)
        )
*/
function http_post ($url, $data)
{
    $data_url = http_build_query ($data);
    $data_len = strlen ($data_url);

    return array ('content'=>file_get_contents ($url, false, stream_context_create (array ('http'=>array ('method'=>'POST'
            , 'header'=>"Connection: close\r\nContent-Length: $data_len\r\n"
            , 'content'=>$data_url
            ))))
        , 'headers'=>$http_response_header
        );
}
?>

But in real application i wouldn't use that approach. I would suggest you to use curl instead.

Simple example for curl:

<?php
  $ch = curl_init(); // create curl handle

  $url = "http://www.google.com";
  /**
   * For https, there are more options that you must define, these you can get from php.net 
   */
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_POST, true);
  curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query(['array_of_your_post_data']));
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3); //timeout in seconds
  curl_setopt($ch,CURLOPT_TIMEOUT, 20); // same for here. Timeout in seconds.
  $response = curl_exec($ch);

  curl_close ($ch); //close curl handle

  echo $response;
?>

Using curl, you would get ur post parameters from $_POST 100% of the times. I have used curl in tens of projects, has never failed me before.

Lauri Orgla
  • 561
  • 3
  • 10
  • First example is exactly the same thing that they did in the post that I followed (wrapped in a function), and curl is not an option for me – user765368 Sep 09 '14 at 21:27
  • You could try to debug the sent headers. To see, if your post headers are actually sent. If they are sent, then try to investigate, why server is not accepting them. – Lauri Orgla Sep 09 '14 at 21:30