10

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?

2 Answers 2

22

try urlencode():

 $film_q   = urlencode($film_name[0]->title);
 $imdb_uri = "http://www.imdbapi.com/?i=&t=$film_q";
Sign up to request clarification or add additional context in comments.

4 Comments

Don't encode the entire url, that'll trash the real metachars. encode ONLY the $film_q part.
Hi, On that I am now getting "Call to undefined function url_encode()"
Brilliant! Thanks a lot guys. I dont think I'd have ever gotten that
works for me with rawurlencode
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.