'CURL response returns string instead of object
I want to get response from YTS api. The first code below works fine, but file_get_contents knows to fail sometimes and I was told to use curl instead. However, with it, I'm getting a string in return instead of object.
I have tried to json_encode then json_decode it but no changes happen at all.
working example with file_get_contents (Here you can see what I want to achieve):
private function getFromApi($url)
{
if (!$data = file_get_contents($url)) {
$error = error_get_last();
throw new Exception("HTTP request failed. Error was: " . $error['message']);
} else {
$data = json_decode($data);
if ($data->status != 'ok') {
throw new Exception("API request failed. Error was: " . $data->status_message);
}
return $data->data;
}
}
curl that gives string instead of object
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://yts.mx/api/v2/list_movies.json?limit=1");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if($e = curl_error($curl)) {
throw new Exception("Curl request failed: " . $e);
}
else {
var_dump($response);
// if ($object->status != 'ok') {
// throw new Exception("API request failed. Error was: " . $object->status_message);
// }
// return $object->data;
}
curl_close($curl);
?>
curl response
string(3279) "{"status":"ok","status_message":"Query was successful","data":{"movie_count":40807,"limit":1,"page_number":1,"movies":[{"id":41604,"url":"https:\/\/yts.mx\/movies\/a-chinese-odyssey-part-one-pandoras-box-1995","imdb_code":"tt0112778","title":"A Chinese Odyssey Part One: Pandora's Box","title_english":"A Chinese Odyssey Part One: Pandora's Box","title_long":"A Chinese Odyssey Part One: Pandora's Box (1995)","slug":"a-chinese-odyssey-part-one-pandoras-box-1995","year":1995,"rating":7.6,"runtime":87,"genres":["Action","Adventure","Comedy"],"summary":"Fantasy adventure about the arrival of Buddhism in China. When the Goddess of Happiness tosses the Longevity Monk and his disciples out of heaven (because the Monkey King tried to attain immortality), the Monkey King is reincarnated as the Joker. He now spends his time chasing two jealous women. When one of them is dying, the Joker goes back in time in an attempt to save her. \u2014Anonymous","description_full":"Fantasy adventure about the arrival of Buddhism in China. When the Goddess of Happiness tosses the Longevity Monk and his disciples out of heaven (because the Monkey King tried to attain immortality), the Monkey King is reincarnated as the Joker. He now spends his time chasing two jealous women. When one of them is dying, the Joker goes back in time in an attempt to save her. \u2014Anonymous","synopsis":"Fantasy adventure about the arrival of Buddhism in China. When the Goddess of Happiness tosses the Longevity Monk and his disciples out of heaven (because the Monkey King tried to attain immortality), the Monkey King is reincarnated as the Joker. He now spends his time chasing two jealous women. When one of them is dying, the Joker goes back in time in an attempt to save her. \u2014Anonymous","yt_trailer_code":"ZPri1X1RVeo","language":"cn","mpa_rating":"","background_image":"https:\/\/yts.mx\/assets\/images\/movies\/a_chinese_odyssey_part_one_pandoras_box_1995\/background.jpg","background_image_original":"https:\/\/yts.mx\/assets\/images\/movies\/a_chinese_odyssey_part_one_pandoras_box_1995\/background.jpg","small_cover_image":"https:\/\/yts.mx\/assets\/images\/movies\/a_chinese_odyssey_part_one_pandoras_box_1995\/small-cover.jpg","medium_cover_image":"https:\/\/yts.mx\/assets\/images\/movies\/a_chinese_odyssey_part_one_pandoras_box_1995\/medium-cover.jpg","large_cover_image":"https:\/\/yts.mx\/assets\/images\/movies\/a_chinese_odyssey_part_one_pandoras_box_1995\/large-cover.jpg","state":"ok","torrents":[{"url":"https:\/\/yts.mx\/torrent\/download\/0575F561D71DD1961F01FC2CBBE22AF5598A6CF1","hash":"0575F561D71DD1961F01FC2CBBE22AF5598A6CF1","quality":"720p","type":"bluray","seeds":0,"peers":0,"size":"810.78 MB","size_bytes":850164449,"date_uploaded":"2022-04-20 20:07:01","date_uploaded_unix":1650478021},{"url":"https:\/\/yts.mx\/torrent\/download\/633A2CAE8DE3C9F50B2E3B89A5BC6304E4770BFE","hash":"633A2CAE8DE3C9F50B2E3B89A5BC6304E4770BFE","quality":"1080p","type":"bluray","seeds":0,"peers":0,"size":"1.63 GB","size_bytes":1750199173,"date_uploaded":"2022-04-20 21:16:40","date_uploaded_unix":1650482200}],"date_uploaded":"2022-04-20 20:07:01","date_uploaded_unix":1650478021}]},"@meta":{"server_time":1650484913,"server_timezone":"CET","api_version":2,"execution_time":"0 ms"}}"
What am I missing? Thanks for your time.
Solution 1:[1]
You get plain JSON string and need to decode it using json_decode
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://yts.mx/api/v2/list_movies.json?limit=1");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$e = curl_error($curl);
curl_close($curl);
if ($e) {
throw new Exception("Curl request failed: " . $e);
}
// PHP 8.0 features in use
$object = json_decode(json: $response, flags: JSON_THROW_ON_ERROR);
if ($object->status != 'ok') {
throw new Exception("API request failed. Error was: " . $object->status_message);
}
return $object->data;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Justinas |
