I call an API url with file_get_contents like this
$options_access_token = array(
'http'=>array(
'ignore_errors' => true,
'method'=>"POST",
'header'=>"Authorization:Bearer mykey"
)
);
$context = stream_context_create($options_access_token);
$access_token = file_get_contents($url_access_token, false, $context);
print_r($access_token);exit;
it give a "HTTP request failed! HTTP/1.0 400 Bad Request"
then i try ignore_errors' => true, on http request and it gave another error, it was "Client sent an HTTP request to an HTTPS server." the url is "http" protocol.
Any ideas what i do wrong or lacking? thanks for any answer it will be helpful
Related
I have a bug in my code.
My code is:
<?php $opts = array(
'http'=>array(
'method'=>"GET",
'max_redirects' => 100
));
$context = stream_context_create($opts);
$url = 'https://fr.socialclub.rockstargames.com/member/m1ssashleex/games/gtav/snapmatic/ajax/search?Filter=MostRecent&page=1';
$file = file_get_contents($url, false, $context);
$vresponse = json_decode($file);
?>
<?php var_dump($vresponse); ?>
I testing all solutions, but i don't have success
error:
Message: file_get_contents(https://fr.socialclub.rockstargames.com/member/m1ssashleex/games/gtav/snapmatic/ajax/search?Filter=MostRecent&page=1): failed to open stream: HTTP request failed! HTTP/1.1 429
1) The error message says HTTP request failed! HTTP/1.1 429. 429 is the error code for too many requests. The server does not deliver the content cause you have exceeded the rate limit.
2) The page you want to get does not work if you are not logged in on that site.
I already tried the following but it doesn't seem to work:
file_get_contents('http://username:password#example.com/requested.file');
And
$auth = base64_encode("username:password");
$context = stream_context_create(['http' => ['header' => "Authorization: Basic $auth"]]);
$homepage = file_get_contents("http://example.com/requested.file", false, $context );
I still keep getting a failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized.
I checked multiple times that the userdata I try to log in with is correct.
I have the script below and was wondering why I was getting the error:
failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required
The base64 encoded value is in the form username:password and is correct. The API documentation gave the example as:
Authorization: Basic
QWxhZGRpbjpvcGVuIHNlc2FtZQ==
For example, if the user agent uses Aladdin as the username and open sesame as the password, then the field is formed as above in the HTTP header
<?php
$postData = array(
'primaryContact' => array('id' => 1),
'subject' => 'Something not working'
);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\r\nContent-Type: application/json",
'content' => json_encode($postData)
)
));
// Send the request
$response = file_get_contents('http://127.0.0.1/', FALSE, $context);
// Check for errors
if($response === FALSE){
die('Error');
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData;
?>
Any ideas?
Tested and your code works. i think its the server. atleast check the server username and password again (your base64 code is correct)
I tested on: iis 7.5 the website has basic auth setup (http 401 Challenge)
(on iis that means that i can use my windows server credentials)
add the $http_response_headers to your error line to get more information.
// Check for errors
if($response === FALSE){
var_dump($http_response_header);
die('Error');
}
If using file_get_contents() to connect to Facebook,
$response = file_get_contents("https://graph.facebook.com/...?access_token=***");
echo "Response: ($response)\n";
And the server returns a non-OK HTTP status, PHP gives a generic error response, and suppresses the response. The body returned is empty.
file_get_contents(...): failed to open stream: HTTP/1.0 400 Bad Request
Response: ()
But if we use cURL, we see that Facebook actually returns a useful response body:
{"error":{"message":"An active access...","type":"OAuthException","code":2500}}
How can I make file_get_contents() return the response body regardless of HTTP errors?
You have to use stream_context_create():
$ctx = stream_context_create(array(
'http' => array (
'ignore_errors' => TRUE
)
));
file_get_contents($url, FALSE, $ctx);
You could ignore the errors file_get_contents throws
$opts = array(
'http'=>array(
'ignore_errors'=> true,
)
);
$context = stream_context_create($opts);
$file = file_get_contents('https://graph.facebook.com/...?access_token=***', false, $context);
var_dump($file);
It seems out of no where my server is acting up. and I'm getting this error.
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
This is my code that I've had implemented for over a year now, and is confirmed working on a separate staging server.
$url = 'http://graph.facebook.com/10150624051911279/photos/all';
$response = json_decode(file_get_contents($url, true));
foreach ($response->data as $photo) {
echo '<li><a href="' . $photo->link . '" ><img class="img" src="timthumb.php?src=' . $photo->source . '&h=175&w=175" /></a></li>';
}
What could possibly be failing on my server to cause this issue. I'm stumped.
The request should fail, because to access that URL you need to obtain an access token. So Facebook returns
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
with along with a "400 Bad Request" status. Additionally since you're opening an URL the second parameter of file_get_contents should be false (there is no point to search the include path, if you know it's not there).
To still get the response from Facebook and ignore the error you can do:
$url = 'http://graph.facebook.com/10150624051911279/photos/all';
$context = stream_context_create(array(
'http' => array(
'ignore_errors'=>true,
'method'=>'GET'
// for more options check http://www.php.net/manual/en/context.http.php
)
));
$response = json_decode(file_get_contents($url, false, $context));