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
Related
I have this file that send a POST request with PHP to a file inside other server:
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
The file that I'm requesting checks if HTTP_X_REQUESTED_WITH is equal to xmlhttprequest. (this is done because most of the requests sent by that file are through ajax)
So how can I add this kind of thing in my PHP code? to be sent this parameter?
You can add the X_Requested_With header to the headers of the http array like so:
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"X-Requested-With: xmlhttprequest\r\n",
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
I'm developing a web application (using GAE for PHP) that notifies android clients (via GCM - Google Cloud Messaging) when some content are available for download.
The following PHP script should do job:
$json = array(
'data' => array( ... ),
'registration_ids' => array( ... )
);
$data = json_encode( $json );
$context = array(
'http' => array(
'method' => 'post',
'header' => 'Authorization: key=MY_SECRET_KEY' . "\r\n" .
'Content-Type: application/json' . "\r\n",
'content' => $data
)
);
$context = #stream_context_create($context);
$result = #file_get_contents("https://android.googleapis.com/gcm/send", false, $context);
The above code runs correctly when the app is deployed, but do not when running on my local development environment.
On local development environment $result is null and the file_get_contents "echo" the following warning failed to open stream: HTTP request failed! HTTP/1.0 405 Method Not Allowed.
I finally figured out what was happening.
The HTTP/1.0 405 Method Not Allowed was related with the 'method' => 'post'.
Belive me, simply changing it to 'method' => 'POST' (note the uppercase!) did the trick.
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?
Can anyone give me any explanation for why this authorization function for a private bitbucket repository is working on my local machine (running PHP Version 5.3.17) but is not authorizing on my remote server (running PHP Version 5.3.20)
I'm not getting an error per se -- i'm just getting a "forbidden" response from bitbucket. But everything works great running from my local server.
function bitBucketConnect($url){
global $bitPassword;
global $bitUsername;
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$bitUsername:$bitPassword")
)
));
// Make the request
return file_get_contents($url, false, $context);
}
Your proxy will respond that authentication is required. You may scratch your head and think "but I'm providing authentication!"
The issue is that the 'header' value is only applicable to http connections. So to authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for using on FTP.
<?php
$opts = array('ftp' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
),
'http' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
$s = file_get_contents("http://www.example.com",false,$context);
$s = file_get_contents("ftp://anonymous:anonymous#ftp.example.org",false,$context);
?>