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.
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.
My question is: how to get 'code' parameter from the response giving by my little piece of code:
$clientID = "clientID...";
$clientSecret = "clientSecret...";
$url = 'https://myweb.com/oauth/authorize/?'.http_build_query(array(
'response_type' => 'code',
'client_id' => $clientID,
'redirect_uri' => 'http://www.mywebhttp.dyndns.org/',
'scope' => array('user')
));
$status = 302;
$response = new RedirectResponse($url,$status,$headers = array());
So what i see going on is that when i'm being given the response, the code parameter exists only in an url in my web explorer, so i can copy it and use. But how to get it directly from php code?
Thank you in advance!
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 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.
I'm trying to use a PHP script to login to another web site (a Moodle install, in this case) by POSTING to the login page.
This code seems to partially work. The problem is that only the first parameter is recognized by the receiving end. For the code shown below, only the 'username' parameter is recognized. However, if you switch the order of 'username' and 'password' so that 'password' is listed first, then only 'password' is recognized using $_POST['password'].
$postdata = http_build_query(
array(
'username' => $currentUser,
'password' => $userPassword,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($loginURL, false, $context);
Any idea on why only the first parameter is being recognized?
Ok, I see now that the code is correct and that something external is interfering with the POST request. This code was being run inside of Drupal so I think that has something to do with it. When I save this separately as a .php file it works as expected.