Making GET request with header in PHP fails - php

I have made the same request in Postman and it gives me the correct response, but when i make the same call from my website it crashes.
Im simply trying to make a GET request from PHP with one header, the oauth2 authorization token.
Code (googleLogin.php):
<?php
$url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Authorization: Bearer " . $_GET["access_token"] . "\r\n"
)
);
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context);
print_r($file);
?>
Error:
PHP Warning:
file_get_contents(https://www.googleapis.com/oauth2/v1/userinfo?alt=json):
failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
in /home/shawn/public_html/cloud/php/googleLogin.php on line 14
I dont know if this error implies (i) The file googleLogin.php on the web server doesnt have correct permissions (i tried 777), (ii) The access token is expired/invalid (it cant be because i tried the same token 2 seconds later successfully with Postman), or (iii) php is formatting my HTTP request incorrectly

Related

php discogs API auth flow

I'm trying to use the discogs php api in a PHP script to get info on a release using the release ID. For example, when I make a request to:
http://mywebsite.com/test.php?id=1017868
I want to call the discogs API to get info on the release with id = 1017868. I can see the info I want by manually going to:
https://api.discogs.com/releases/1017868
So I have my $consumerKey =and $consumerSecret and I'm following the DISCOGS AUTH FLOW instructions, which says I can send my keys in a get request like so:
curl "https://api.discogs.com/database/search?q=Nirvana" -H "Authorization: Discogs key=foo123, secret=bar456"
I'm trying to make a get request for my target id in my php script like so:
<?php
echo "hello world <br>";
//discogs simple auth flow, http requests
$remote_url = 'https://api.discogs.com/releases/1017868';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Discogs key=mykeyasdlkhaskld, secret=mysecretkeykjnasdkjnsadkj"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);
print($file);
echo "end of program";
?>
But I keep getting the error:
"failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden".
Is there something I'm forgetting for making my request to the server? Thanks.

"No 'Access-Control-Allow-Origin' header is present on the requested resource" error when sending request to Jawbone UP API

I've built a custom database that I would like to populate with some of my UP3 data. I've successfully authenticated and received the JSON response with my Bearer token. From here, I'm sort of lost.
From the documentations, I need to send a GET request to
https://jawbone.com/nudge/api/v.1.1/users/#me
Each time I do, I get:
No 'Access-Control-Allow-Origin' header is present on the requested resource
I've added:
header('Access-Control-Allow-Origin: http://example.com');
(replacing example.com with my domain)
to the page that is sending the GET request. Any thoughts?
The Jawbone endpoints all have the CORS header set to
Access-Control-Allow-Origin: *
However, this still will not allow cross-site requests from localhost (thanks #TirthrajBarot for the explanation in the comments), so you should disable this check in Chrome with something like the Access-Control-Allow-Origin extension.
After authenticating the user and permissions, I redirect to another page to validate the authentication code and get the access token. From there, I go to another page to send the GET request for the data. Here is the code that is working:
$url = "https://jawbone.com/nudge/api/v.1.1/users/#me/sleeps?" . $sleepDate;
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/json\r\n" .
"Authorization: Bearer " . $accessToken . "\r\n" .
"Host: {my website}"
)
);
$context = stream_context_create($options);
$sleepData = #file_get_contents($url, false, $context);
The data is returned, decoded, and processed. The result is pushed into a text box in a form for the user to edit if desired. There is also a button to enter the data into the database (along with a bunch of hidden form fields that enter other relevant details)

How to attach authorization header via PHP?

I am using PHP to get JSON from a remote server via file_get_contents command. Here is the piece of code I used:
$opts = array(
'https'=>array(
'method'=>'GET',
'header'=>'Accept-language: en\r\n' .
'Authorization: MAC ["3","ios2.5.0","123","123abc","123=","abc="]\r\n' .
'User-Agent: abc/1.1.1 iOS/10.0.2 iPhone/iPhone7,1\r\n'
)
);
$context = stream_context_create($opts);
$file = file_get_contents('https://www.google.com/v11/file?search=ios&with=users%2Cfiles%2Cquestions', false, $context);
echo $file;
I did a quick debugging:
Using Postman I was able to get the json file with the same header.
I tried a different json from a different url, it works.
I tried a local file, it works.
You have to understand what file_get_contents is. This command is a request to get the file on the server, in this case it is requesting to get https://www.google.com/v11/file/index.html on the server as in one single step. Since your url seems to use header to verify your origin, it might be an ajax request, meaning the server components didn't set up to allow an output from file_get_contents requests, instead they probably accept cURL requests.
So you can use:
curl_exec()

Receiving HTTP header 406 error

I am trying to invoke http://dbpedia.org/page/Los_Angeles in PHP with HTTP headers for an educational assignment I have been given. It must be this URL i.e. I am not allowed to use the JSON URL directly.
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Accept: application/json"
)
);
$context = stream_context_create($opts);
$url = "http://dbpedia.org/page/Los_Angeles";
$data = file_get_contents($url,false,$context);
echo $data;
?>
I'm facing this error:
Warning: file_get_contents(http://dbpedia.org/page/Los_Angeles): failed to open stream: HTTP request failed! HTTP/1.1 406 Unacceptable.
Please help to get rid of this error. Note: I read about this error and found suggestions to use CURL. However, I don't want to go into installation of CURL. Kindly suggest using file_get_contents.
406 says that application/json is not available.
From Wikipedia:
406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
<?php
$url = "http://dbpedia.org/page/Los_Angeles";
$data = file_get_contents($url);
echo $data;
?>

PHP POST with custom headers giving HTTP 400 Bad Request Response (Bug?)

I'm having some trouble POSTing data from a client machine to our internal site using PHP. The server accepts data via HTTPS with basic authentication. Here is the code I'm using to POST with:
$parameters = array('http' => array(
'method' => 'POST',
'content' => $data
)
);
if ($optionalHeaders !== NULL) {
$parameters['http']['header'] = $optionalHeaders;
}
$ctx = stream_context_create($parameters);
$fp = fopen($url, 'rb', false, $ctx);
With the following header:
$postHeader = "POST /index.php HTTP/1.1\r\n".
"Host:my.host\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"User-Agent: PHP-Code\r\n".
"Content-Length: " . strlen($postData) . "\r\n".
"Authorization: Basic ".base64_encode($user.':'.$password)."\r\n".
"Connection: close\r\n";
Now, I can get this to work, and it posts just fine on one of my clients with PHP version 5.2.5, but the on another client I get this error message:
fopen(magical_url): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
And Apache error log gives:
request failed: error reading the headers
The only difference I can see is that the latter client has PHP version 5.1.6.
Does anyone know if this is a bug? Or am I doing something wrong somewhere...
I've looked through the PHP site and found this bug listed for version 5.2.6 of PHP https://bugs.php.net/bug.php?id=45540 but this post-dates the version it works on!
Thanks,
Jak
You should not provide all those headers. That's bound to fail.
Headers like POST, Host, Content-Length and Connection are automatically provided. Remove all your optional headers (should work then) and add them step by step.

Categories