简体   繁体   中英

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

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?

try urlencode() :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM