user3244721
user3244721

Reputation: 754

HTTP/1.1 400 Bad Request in file_get_contents

I have a link herehttp://www.example.com/Movies.aspx?movname=Raja Natwarlal

Here is my code

file_get_contents($link, false, $context);

It give me this error

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

I also tried this code

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get the code of request
curl_close($ch);

It give me $httpCode=400

Upvotes: 2

Views: 5753

Answers (1)

arkascha
arkascha

Reputation: 42925

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:

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.

Upvotes: 3

Related Questions