PHP $_GET parameters give issues - php

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

Related

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.

Failed to open stream: HTTP request failed - 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 ?

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.

PHP post request returns 400 error

I have the following class, it's using a web service to send a request and get a JSON response back.
When i try the same thing with Chrome's Postman extension, I get a nice little response back.
class RequestController {
function __construct() {
global $config;
$this->config = $config;
}
function send_request() {
$url = $this->config['GATEWAY']['url'] . 'getbyquery';
$json_data = '{"query":{"AllFields":true,"ConditionSetOperator":0,"ConditionSets":[],"Distinct":false,"Fields":["jay_alb","jay_levelms"],"Links":[],"Orders":[],"RecordType":"jay_lifeinsurance","Top":0}}';
$data = array($json_data);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
print "<pre>";
print_r($options);
print "</pre>";
$result = file_get_contents($url, false, $context);
$data = json_decode($result);
var_dump($data);
}
}
However when I'm using my class I get an error.
Warning: file_get_contents(http://crm-gateway.premier.com.au/GatewayService.svc/getbyquery) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\crm\includes\classes\RequestController.php on line 32
Can you please point out what I'm doing wrong?
Thanks,

Make file_get_contents() return server response despite HTTP errors

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

Categories