0
votes

I sent values through file_get_contents.
My question is i am unable receive(print) GET method values in work.php.
I am using stream_context_create() this will create a resource id.

page name sendvalues.php

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'phone' => "9848509317",
    'msg'   => "hi naveen"
  )
);

echo $context = stream_context_create($opts);

$file = file_get_contents('http://www.aakrutisolutions.com/projects/testingsite/smstest/sms_http_curl/work.php', false, $context);
echo $file;

page name work.php
echo "
";
print_r($_GET);  /// i am unable to get my query string values
echo "
";
2
i observed few points like way of asking question should be clear. am i correct?Navruk

2 Answers

3
votes

Just append your parameters url encoded to your url:

$file = file_get_contents('http://www.aakrutisolutions.com/projects/testingsite/smstest/sms_http_curl/work.php?phone=123&msg=hi%20naveen');

The context options you used... are context options. Here is specified which you can user for http: http://www.php.net/manual/de/context.http.php It's not to be transmitted if you put random stuff in there.

2
votes

Your array is incorrect. Look at the examples at http://php.net/manual/en/function.file-get-contents.php. You cannot simply add POST arguments to the context array.

The correct context array for a POST request would look like this:

$opts = array(
  'http' => array(
    'method' => 'POST',
    'content' => http_build_query(array(
      'phone' => 9848509317,
      'msg'   => 'hi naveen'
    ))
  )
);

Or simply use GET (as you expect in your other script), so put the arguments in the URL (build the query string with http_build_query()).