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 "";
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.
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()).
Related
Before I begin with my question, I will mention that I am re-learning PHP after a long time away from the language. Please be gentle. Also, I know that I could use a library like curl to do some of these things, but I would like to understand how PHP works natively.
I am trying to submit an http GET request to a Microsoft API (Identity Platform). The following is my code:
<?php
$data = array (
'client_id' => '6731de76-14a6-49ae-97bc-6eba6914391e',
'state' => '12345',
'redirect_uri' => urlencode('http://localhost/myapp/permissions')
);
$streamOptions = array('http' => array(
'method' => 'GET',
'content' => $data
));
$streamContext = stream_context_create($streamOptions);
$streamURL = 'https://login.microsoftonline.com/common/adminconsent';
$streamResult = file_get_contents($streamURL, false, $streamContext);
echo $streamResult;
?>
When I try and execute the above code, I get this:
Error snip
Conversely, with the following code, the http request works fine:
<?php
$streamURL = 'https://login.microsoftonline.com/common/adminconsent?client_id=6731de76-14a6-49ae-97bc-6eba6914391e&state=12345&redirect_uri=http://localhost/myapp/permissions';
$streamResult = file_get_contents($streamURL);
echo $streamResult;
?>
Can anyone provide insight as to why the first example fails while the second succeeds? My thought is that there must be some kind of syntactical error. Thanks in advance.
The content parameter is for the request body, for POST and PUT requests. But GET parameters don't go in the body, they go right on the URL. So your first example is simply making a GET request to the base URL with no parameters at all. Note also that the method parameter already defaults to GET, so you can just skip the whole streams bit.
You can build your URL like:
$urlBase = 'https://login.microsoftonline.com/common/adminconsent';
$data = [
'client_id' => '...',
'state' => '12345',
'redirect_uri' => 'http://localhost/myapp/permissions',
];
$url = $urlBase . '?' . http_build_query($data);
And then just:
$content = file_get_contents($url);
Or just cram it all into one statement:
$content = file_get_contents(
'https://login.microsoftonline.com/common/adminconsent?' .
http_build_query([
'client_id' => '...',
'state' => '12345',
'redirect_uri' => 'http://localhost/myapp/permissions',
])
);
Or use $url to feed curl_init() or Guzzle or similar.
I'm trying to access a webservice. I'm already using this webservice from an android app but now I need to access some functions from a php document. If I use chromes advanced rest client application for testing it works fine if I select the POST option and application/x-www-form-urlencode as content-type. But when I try to access the webservice from my PHP file I get the response from the server that it can't find the value "tag". This is the code:
$data = array( 'tag' => 'something');
$options = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' => "Content-Type: application/x-www-form-urlencode")
);
$context = stream_context_create($options);
$url = 'myurl';
$result = file_get_contents($url,false,$context);
$response = json_decode($result);
What is wrong with this code?
Thanks for any help!
Try this:
$data = http_build_query( array( 'tag' => 'something') );
As defined here, "Content" value must be a string: http_build_query generate the URL-encoded query string you need.
How can I send data to a specific port and ip using yii framework?
I want send an string to specific ip.
Using php you can send data using GET or POST or whatever method you like using this method:
<?php
// change this to your own values
$ip = '127.0.0.1';
$port = 80;
$location = '';
$url = "http://$ip:$port/$location";
// define the data you want to send
$params = http_build_query(
array(
'name1' => $value1,
'name2' => $value2,
// ...
)
);
// set the method to send the data
$opts = array('http' =>
array(
'method' => 'GET', // or POST or PUT or DELETE
'content' => $params,
'header' => 'Content-Type: application/x-www-form-urlencoded\r\n'
)
);
$context = stream_context_create($opts); //build the http context
// send the data and capture the response
$response = file_get_contents($url.$params, false, $context);
I try to make google url shortener with wp_remote_post()
but I got error result,
I know how to use CURL, but CURL not allowed in WordPress!
This resource for API with WordPress:
http://codex.wordpress.org/Function_Reference/wp_remote_post
http://codex.wordpress.org/Function_Reference/wp_remote_retrieve_body
http://codex.wordpress.org/HTTP_API#Other_Arguments
http://codex.wordpress.org/Function_Reference/wp_remote_post#Related
This google url shortener API docs:
https://developers.google.com/url-shortener/v1/getting_started#shorten
This is my code:
function google_url_shrt{
$url = 'http://example-long-url.com/example-long-url'; // long url to short it
$args = array(
"headers" => array( "Content-type:application/json" ),
"body" => array( "longUrl" => $url )
);
$short = wp_remote_post("https://www.googleapis.com/urlshortener/v1/url", $args);
$retrieve = wp_remote_retrieve_body( $short );
$response = json_decode($retrieve, true);
echo '<pre>';
print_r($response);
echo '</pre>';
}
The WordPress API requires that the headers array contain an element content-type if you want to change the content type of a POST request.
Also, it looks like the body of your HTTP request is being passed as a PHP array, not as a JSON string as the Google Shortener API requires.
Wrap the array definition for body in a json_encode statement, and make the headers field a sub-array, and give it a shot:
$args = array(
'headers' => array('content-type' => 'application/json'),
'body' => json_encode(array('longUrl' => $url)),
);
Alternative, you could just write the JSON format yourself as it is fairly simple:
$args = array(
'headers' => array('content-type' => 'application/json'),
'body' => '{"longUrl":"' . $url . '"}',
);
I want to send a GET request to an external site, but also want to send some parameters
for example i've to send a get request to example.com
i want to execute www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz
My code is :
$getdata = http_build_query(
array(
'uid' => '1',
'pwd' => '2',
'msg'=>'3',
'phone'=>'9999',
'provider'=>'xyz'
)
);
$opts = array('http' =>
array(
'method' => 'GET',
'content' => $getdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/send.php', false, $context);
I get a server error .
The content option is used with POST and PUT requests. For GET you can just append it as a query string:
file_get_contents('http://example.com/send.php?'.$getdata, false, $context);
Furthermore, the method defaults to GET so you don't even need to set options, nor create a stream context. So, for this particular situation, you could simply call file_get_contents with the first parameter if you wish.