简体   繁体   中英

How to properly retrieve text from decoded json PHP

Im trying to get an image link from a JSON file which I can retrieve by a link like this the problem I am having is extracting the correct data. I want to get the field poster_path which is inside the results array however my attempts have been unsuccessful. Currently this is my code -

$json = 'https://api.themoviedb.org/3/search/movie?api_key=15d2ea6d0dc1d476efbca3eba2b9bbfb&query='.$title;
$json = preg_replace('/\s+/', '%', $json);
$json = file_get_contents($json);
$obj = json_decode($json);
echo $obj->results[0]['poster_path'];
//Have also tried
//echo $obj->results[0]->poster_path;

It works if I only try to reach the, for example total_pages data however im unable to access the results array.

EDIT - Turns out the results array doesn't actually pick up its contents from the link, the JSON is left like this - {"page":1,"total_results":0,"total_pages":1,"results":[]}

You need to use object syntax to access the property instead:

$obj->results[0]->poster_path

will return the data you want.

You also have a problem with your url-encoding. Your approach using preg_replace is wrong (should be %20 not just %) but it's also naive because it doesn't account for other possible encoding issues in the input. PHP has a built-in function urlencode ( http://php.net/manual/en/function.urlencode.php ) which is designed to take care of this kind of thing for you.

And lastly you have an odd variable naming - the use of "$json" for your url string doesn't make any sense, and also you already use that name further down for the response from the remote server, which makes a lot more sense. Variables with different purposes should be named according to their purpose and also not conflict / overwrite each other. This will make your code more maintainable ingeneral.

Here's a better version of your code:

$url = 'https://api.themoviedb.org/3/search/movie?api_key=15d2ea6d0dc1d476efbca3eba2b9bbfb&query='.urlencode($title);
$json = file_get_contents($url);
$obj = json_decode($json);
echo $obj->results[0]->poster_path;
$obj->results[0]->poster_path;

should work well. But some object has a null inside that attribute. Eg:

{
  "vote_count": 0,
  "id": 479701,
  "video": false,
  "vote_average": 0,
  "title": "Test",
  "popularity": 4.92,
  "poster_path": null,
  "original_language": "en",
  "original_title": "Test",
  "genre_ids": [
    28
  ],
  "backdrop_path": null,
  "adult": false,
  "overview": "Testing the APIs",
  "release_date": "2017-10-05"
}

If you want to get array instead object, you shoul to set true as second parameter of json_decode :

$obj = json_decode($json, true);

Then you can get your data as:

echo $obj['results'][0]['poster_path'];
$json = 'https://api.themoviedb.org/3/search/movie?api_key=15d2ea6d0dc1d476efbca3eba2b9bbfb&query=Raiders%25of%25the%25lost%25ark';
        $json = preg_replace('/\s+/', '%', $json);
        $json = file_get_contents($json);
        $obj = json_decode($json);
        $result = array();
        foreach($obj->results as $key=>$value)
        {
            $result[$value->id] = $value->poster_path;
        }
        echo $result;

i hope this is what you are searching for!! if not please update what output are you expecting

显然,这不是JSON的问题,但是prag_replace用%而不是%20替换了网址中的空格,这意味着它找不到结果,所以失败了。

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