Failed to open stream: HTTP request failed - PHP - php

I have this php code and I'm trying to get content of image.jpg from url but I need to athuraize my self to the server and I did use the fowling code:
$user = 'username';
$pass = 'password';
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$user:$pass")
)
));
$data = file_get_contents("http://mysite/User%20Photos/Profile%20Pictures/bmir_LThumb.jpg", false, $context);
My problem still getting this error:
failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
I saw many situations like me and I tried their solution like this one but still having problem .
Any help ?

Related

PHP $_GET parameters give issues

Hi: I don't understand where I'm making errors, I'm calling fopen with an url
I have this function that should do POST requests:
function do_post(string $url, $params=""): string {
$options = array(
'http' => array(
'header' => "Length: ".strlen($params)."\r\n"." Content-Type: application/json\r\n",
'method' => 'POST',
'content' => $params
)
);
$context = stream_context_create($options);
$fp = fopen($url,'rb',false,$context);
if(!$fp) {
throw new ErrorException("post error - fopen");
}
$ret = #stream_get_contents($fp);
if(!$ret) {
throw new ErrorException("post error - get contents");
}
return $ret;
}
I call it like:
do_post('http://127.0.0.1:9191/dummyresource/DUMMYOUT.php?user=u1&status=ON');
But I got this error:
fopen(http://127.0.0.1:9191/dummyresource/DUMMYOUT.php?user=u1&status=ON):
failed to open stream: HTTP request failed! HTTP/1.1 411 Length
Required
I also added this line just before the fopen call but does nothing:
$url = str_replace('&', "&", $url);
How could I fix?
EDIT:
tested with Content-Length
I got
PHP Warning:
fopen(http://127.0.0.1:9191/dummyresource/DUMMYOUT.php?user=u1&status=ON):
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

Bug json decode API

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.

Doing a http authentication call with php

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.

PHP: failed to open stream: HTTP request failed

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');
}

CSide API - Warning: file_get_contents(http://cside.pt/UIAPI/token): failed to open stream: HTTP request failed

Anyone ever used CSide API? (http://www.cside.pt/UIAPI/)
I don't know what's the problem, it might be my code or the credentials.
<?php
$api_url = 'http://cside.pt/UIAPI/token';
$client_id = 'xxx';
$client_secret = 'xxx';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Authorization: Basic"."\r\n".
base64_encode("$client_id:$client_secret")
)
);
$context = stream_context_create($opts);
$result = file_get_contents($api_url, false, $context);
print_r($result);
?>
I'm getting this error
Warning: file_get_contents(http://cside.pt/UIAPI/token): failed to open stream: HTTP request failed! in C:\xampp\htdocs\wetrig\CI\api\test.php on line 18
Fatal error: Maximum execution time of 30 seconds exceeded in
C:\xampp\htdocs\wetrig\CI\api\test.php on line 18
I really appreciate your help.

Categories