Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

php

I have built a local DVD Database using Codeigniter, With film names etc in. What I am looking to do is load in more data for the film from this IMDB API.

However I am getting the following error :

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://www.imdbapi.com/?i=&t=2 Fast 2 Furious) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

Filename: controllers/dvd.php

Line Number: 92

Copying this URI into an address bar returns the json data, But when loading in my page it gives me that error.

It is worth noting I am running on a localhost this particular site. As I am just familiarising myself with Codeigniter.

My code is....

            // Send Query To IMDB Api

        $film_q = $film_name[0]->title;
        $imdb_uri = "http://www.imdbapi.com/?i=&t=$film_q";
        $json = file_get_contents($imdb_uri);
        $json = json_decode($json);

        print_r($json);

EDIT : Looking further into this. Single word film names seem to load in ok, So I'm assuming I need to someway drill down a "+" into the spaces?

like image 984
StuBlackett Avatar asked Jun 16 '12 17:06

StuBlackett


2 Answers

try urlencode():

 $film_q   = urlencode($film_name[0]->title);
 $imdb_uri = "http://www.imdbapi.com/?i=&t=$film_q";
like image 179
mgraph Avatar answered Oct 20 '22 04:10

mgraph


You might consider running urlencode() on the movie title. It's possible that the spaces in "2 Fast 2 Furious" were left behind.

Your link should look like this.

http://www.imdbapi.com/?i=&t=2%20Fast%202%20Furious

Not this.

http://www.imdbapi.com/?i=&t=2 Fast 2 Furious
like image 2
Sage Gerard Avatar answered Oct 20 '22 03:10

Sage Gerard