PHP set protocol_version for stream_context_create - php

I want to access an oauth server with a php script, the remote server requires the following request:
POST /oauth/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 81
Authorization: Basic CHAINE_ENCODEE
grant_type=password&username=USERNAME&password=PASSWORD&scope=all
So I wrote the following code:
$params = array ('grant_type' => 'password',
'username' => 'USERNAME',
'password' => hash('sha256', 'PASSWORD'),
'scope' => 'all',
);
$query = http_build_query ($params);
$contextData = array (
'protocol_version'=> '1.1',
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: 81\r\n".
"Authorization: Basic ".base64_encode('APPIDENTIFIER:SECRET'),
'content' => $query );
$context = stream_context_create (array ( 'http' => $contextData ));
$result = file_get_contents (
'https://rest2.some-url.net/oauth/token', // page url
false,
$context);
return $result;
But this gives me the following error:
Warning: file_get_contents(https://rest2.some-url.net/oauth/token): failed to open stream:
HTTP request failed! HTTP/1.0 500 Internal Server Error
My guess is that the problem comes from the protocol_version which I set to 1.1 as required by the api. But the error apparently uses HTTP/1.0
I have tried every possible way, i have seen no one saying it was not working for them so I guess I am missing something. Why is not the HTTP set to HTTP/1.1?

Related

Unauthorized Mirosoft Graph request? Calendar.Read

I am trying to execute the following Microsoft Graph request:
$url = 'https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2020-04-17T12:13:36.933Z&enddatetime=2020-04-24T12:13:36.933Z';
$data = array('grant_type' => 'authorization_code', 'client_id' => '<myclientid>', 'client_secret' => '<myclientsecret>', 'redirect_uri' => 'http://localhost/myapp/request.php', 'code' => '<myauthorisationcode>');
$options = array(
'http' => array(
'header' => "Authorization: Bearer <myaccesstoken>",
'header' => "Host: login.microsoftonline.com",
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
Actually I should get information about my calendar now. But I receive the following warning:
Warning: file_get_contents(https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2020-04-17T12:13:36.933Z&enddatetime=2020-04-24T12:13:36.933Z): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\xampp\htdocs\myapp\request.php on line 16
bool(false)
In Azure I have added the following 3 API permissions: Calendars.Read, User.Read, User.Read.All.
my API Authorizations
Does anyone know why my request has not been accepted?
Thanks for your help!

PHP POST request raising HTTP/1.0 instead of HTTP1.1

I am new to PHP. I am trying to make a HTTPS POST request from PHP to an external server. Based on the posts on the Internet I have the following code:
// Create POST data.
$post_data = array(
'roomId' => $space_token,
'text' => $message
);
$output .= json_encode($post_data);
// Creating context
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Bearer {$api_access_token}\r\n".
"Content-Type: application/json\r\n",
'content' => json_encode($post_data)
)
));
$response = fopen($api_url, 'r', false, $context);
However, the request is made with HTTP/1.0 instead of HTTP/1.1, and the server that i am trying to reach returns 502 Bad Gateway response when it receives HTTP/1.0 requests.
PHP Version: 5.4.16
HTTPD Version: Apache/2.4.6 (CentOS)
Not sure what I am doing wrong.
You need insert to header 'protocol_version' => 1.1

HTTP request failed! with file get contents

I used OAuth for Dropbox to get access token: https://blogs.dropbox.com/developers/2012/07/using-oauth-1-0-with-the-plaintext-signature-method/
But I got error message:
Warning: file_get_contents(https://api.dropbox.com/1/oauth/request_token): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
My PHP Code:
$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));
$Options = array('http' =>
array(
'method' => 'POST',
'header' => $Header,
)
);
$Context = stream_context_create($Options);
$Result = file_get_contents("https://api.dropbox.com/1/oauth/request_token", false, $Context);
print_r($Result);
Based on the documentation at http://php.net/manual/en/context.http.php, it looks like the header option is just a normal string, not json_encoded. Alternatively, you should be able to use a numerically indexed array of headers, like so:
$Header = array(
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"XXXXX\", oauth_signature=\"XXXXX&\"\r\n"
);
$Options = array('http' =>
array(
'method' => 'POST',
'header' => $Header,
)
);
This is wrong:
$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));
The header paramter in an http stream is either a simple single string containing one single header key: value, or an array of key: value strings. You're stuffing in a single json string as your header, meaning it's NOT a valid http header.
Remove the json_encode() part completely. $Header = array(...) is all you need.

Bad request using file_get_contents for PUT request in PHP

This api call works fine using Postman (a REST Client),
but when making the request on the server in my GAE application I am currently getting the following error:
HTTP request failed! in C:\Projects\app\file.php on line 26
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\Projects\app\file.php on line 26
Here is the code from my application, which makes a call to the rest api method /exampleMethod
$dataraw = [
'email' => 'email#ex.com',
'first_name' => 'firstname',
'last_name' => 'lastname',
'fEmail' => 'email#ex.com',
'message' => 'test'
];
$data = http_build_query ( $dataraw );
$context = [
'http' => [
'method' => 'PUT',
'header' => "Authorization: apikeystring\r\n" . "Content-Length: " . strlen($data) . "\r\n" . "Content-Type: application/json\r\n",
'content' => $data,
'proxy' => 'tcp://euproxy.example.com:0000'
]
];
$context = stream_context_create ( $context );
$result = file_get_contents ( $GLOBALS ['REST_API_URL'] . '/exampleMethod', false, $context );
In the application I have successfully used file_get_contents for GET requests, but with this PUT request I am getting a 400 bad request which is not coming from the Rest Api.
Change mb_strlen(serialize($dataraw), '8bit') to strlen($data)

How to send a facebook notification to a user without using the SDK with only PHP

I don't know if this is possible, i'm having a problem getting the PHP SDK to work and i don't want to send App Access Token to browser.
So i tried the following.
$contextData = array (
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
/* 'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query*/ );
$context = stream_context_create (array('https' =>$contextData));
$url = 'https://graph.facebook.com/userAppId/notifications?access_token=********&href=index.php&template=test';
$result = file_get_contents ($url,false,$context);
echo $result;
Problem: I get this error.
Warning: file_get_contents(https://graph.facebook.com/userAppId/notifications?
access_token=********&href=index.php&template=test):
failed to open stream:HTTP request failed! HTTP/1.1 400 Bad Request in
C:\wamp\app\fb\fb_notificationsphp.php on line 19

Categories