PHP Get Content of Http 400 Response

PHP Get Content of HTTP 400 Response

You have to define custom stream context (3rd argument of function file_get_contents) with ignore_errors option on.

PHP file_get_contents returns with a 400 Error

You need to set user agent for file_get_contents like this, and you can check it with this code. Refer to this for set user agent for file_get_contents.

<?php
$options = array('http' => array('user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0'));
$context = stream_context_create($options);
$response = file_get_contents('https://owapi.net/api/v3/u/Xvs-1176/blob', false, $context);
print_r($response);

file_get_contents throws 400 Bad Request error PHP

You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:

// make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

// convert response
$output = json_decode($output);

// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {

var_dump($output);
}

curl_close($ch);

This may give you a better idea why you're receiving the error. A common error is hitting the rate limit on your server.

Why do I get a 400 Bad Request error from api

The documentation says that
you need to pass the tracking_reference parameter on the query string (just as you did with Postman):

https://api.shiplogic.com/tracking/shipments?tracking_reference=G9G

But that's not what your code does:

$data = '{"tracking_reference": "M3RPH"}';
$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri, ["content-type"=>"application/json"], $data)

The exception message clearly shows that the parameter is missing from the URL:

`GET https://api.shiplogic.com/tracking/shipments` resulted in a `400 Bad Request` response

You should do something like this instead:

$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri.'?tracking_reference=M3RPH');

Using PHP curl how does one get the response body for a 400 response

You can unset CURLOPT_FAILONERROR for once. And adding your error status code to CURLOPT_HTTP200ALIASES as expected might also help.

 curl_setopt($conn, CURLOPT_FAILONERROR, false);
curl_setopt($conn, CURLOPT_HTTP200ALIASES, (array)400);

(libcurl also has a CURLOPT_ERRORBUFFER, but you cannot use that option from PHP.)

Btw, curl behaves correctly by not returning the response body in case of 4xx errors. Not sure if that can be overriden. So you might have to migrate to PEAR HTTP_Request2 or a similar HTTP request class where you can deviate from the standard.

HTTP/1.1 400 Bad Request in file_get_contents

The URL you posted is not valid in a technical way. A browser will take care of that if you enter such a URL, but on a more technical level you have to deal with such details yourself.

In this case there is a blank inside the urls request parameter. Such a character is not valid in a URL. Therefore you have to escape it. Have a try with these variants:

  • http://www.example.com/Movies.aspx?movname=Raja+Natwarlal

  • http://www.example.com/Movies.aspx?movname=Raja%20Natwarlal

Note that you cannot simply use a function like urlencode() to process the whole URL. That would also escape things like slashes and the like. That function is meant to escape a string such that it can be used as a single token inside an URL.



Related Topics



Leave a reply



Submit