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.
Related
I'm following this documentation: https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code
I have just completed this step (by simply visiting the URL below):
https://login.microsoftonline.com/{tenant}/oauth2/authorize?client_id={client_id}&response_type=code
Which redirected me to my redirect URL with code query string attached:
https://example.com/?code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCm.................................
At the end of the code query string above, there was an additional session_state parameter.
The next step outlined in the link at the top of this question says to make a POST request, but I'm having trouble forming this call.
Here's what the docs represent being an example:
How would I form and call this request in PHP (without using cURL)?
Here's my attempt, but I don't know whether or not I'm correct:
$url = 'https://login.microsoftonline.com/{tenant}/oath2/token';
$data = array( 'grant_type' => 'authorization_code',
'client_id' => '2d4d11a2-f814-46a7-890a-274a72a7309e',
'code' => 'AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCm...................',
'redirect_uri' => 'https://example.com',
'resource' => 'https://graph.microsoft.com',
'client_secret' => '{client_secret}' );
$options = array(
'http' => array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$var_dump($result);
UPDATE: The code above (when executed) returns a 500 Internal Server Error.
Also, I don't know whether or not I should be adding session_state (mentioned above) into the POST call.
The URL should be https://login.microsoftonline.com/{tenant}/oauth2/token.
You have to add the client secret to the request, as well as the resource.
If you are trying to call Microsoft Graph for example, the resource should be https://graph.microsoft.com.
I am trying to parse a webpage using file_get_contents. For method post, I am able to parse which is the first page in the list. After this, the page contents are listed via pagination.
How can we parse contents of paginated pages with the GET request?
For example : http://sample.com/results?page=3
Code:
$data = array(
'page' => 1
);
$opts = array(
'http'=>array(
'method'=> 'GET',
'headers' => $http_response_header,
'content' => http_build_query($data),
'timeout' => 100
)
);
$context = stream_context_create($opts);
$listfile1 = file_get_contents("http://sample.com/results?",false, $context);
I get 0 records for the paginated page.
I was able to use the same function listed above, There was a custom function defined which was breaking the pagination.
Yet I have changed the url also.
$listfile1 = file_get_contents("sample.com/results?page=".$number,false, $context);
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.
I need to validate some form data both in client-side and server-side (php). I can use some API web service to check the data form. The problem is: I can validate the data, in client side, using an Ajax call and my API web service. How can I use the API web service from server-side?
You can use file_get_contents to invoke external call
$homepage = file_get_contents('http://www.example.com/');
And there you can send the parameter from the url itself,
Here the example to send parameter to your api
Construct an array and place your values to be validated
$getdata = http_build_query(
array(
'name' => 'Francesco',
'phone' => '2434343',
'age'=>'23',
'city'=>'MA',
'state'=>'US'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $getdata
)
);
$context = stream_context_create($opts);
Then send it to your api's directly
file_get_contents('http://example.com/send.php?'.$getdata, false, $context);
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()).