I am trying to get response from YTS API, however I can't understand what went wrong here. I do believe that status
and status_message
are objects.
Errors
PHP Notice: Trying to get property 'status' of non-object in /var/www/html/movies/inc/YTS.php on line 232
Notice: Trying to get property 'status' of non-object in /var/www/html/movies/inc/YTS.php on line 232
PHP Notice: Trying to get property 'status_message' of non-object in /var/www/html/movies/inc/YTS.php on line 233
Notice: Trying to get property 'status_message' of non-object in /var/www/html/movies/inc/YTS.php on line 233
PHP Fatal error: Uncaught Exception: API request failed. Error was: in /var/www/html/movies/inc/YTS.php:233
Code:
$url = "https://yts.mx/api/v2/list_movies.json?limit=1";
private function getFromApi($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if($e = curl_error($curl)) {
throw new Exception("Curl request failed: " . $e);
}
else {
$data = json_decode($response);
if ($data->status != 'ok') {
throw new Exception("API request failed. Error was: " . $data->status_message);
}
return $data->data;
}
curl_close($curl);
}
var_dump($data)
object(stdClass)#1 (4) { ["status"]=> string(2) "ok" ["status_message"]=> string(20) "Query was successful" ["data"]=> object(stdClass)#2 (4) { ["movie_count"]=> int(40807) ["limit"]=> int(1) ["page_number"]=> int(1) ["movies"]=> array(1) { [0]=> object(stdClass)#3 (26) { ["id"]=> int(41604) ["url"]=> string(66) "https://yts.mx/movies/a-chinese-odyssey-part-one-pandoras-box-1995" ["imdb_code"]=> string(9) "tt0112778" ["title"]=> string(41) "A Chinese Odyssey Part One: Pandora's Box" ["title_english"]=> string(41) "A Chinese Odyssey Part One: Pandora's Box" ["title_long"]=> string(48) "A Chinese Odyssey Part One: Pandora's Box (1995)" ["slug"]=> string(44) "a-chinese-odyssey-part-one-pandoras-box-1995" ["year"]=> int(1995) ["rating"]=> float(7.6) ["runtime"]=> int(87) ["genres"]=> array(3) { [0]=> string(6) "Action" [1]=> string(9) "Adventure" [2]=> string(6) "Comedy" } ["summary"]=> string(391) "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. —Anonymous" ["description_full"]=> string(391) "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. —Anonymous" ["synopsis"]=> string(391) "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. —Anonymous" ["yt_trailer_code"]=> string(11) "ZPri1X1RVeo" ["language"]=> string(2) "cn" ["mpa_rating"]=> string(0) "" ["background_image"]=> string(95) "https://yts.mx/assets/images/movies/a_chinese_odyssey_part_one_pandoras_box_1995/background.jpg" ["background_image_original"]=> string(95) "https://yts.mx/assets/images/movies/a_chinese_odyssey_part_one_pandoras_box_1995/background.jpg" ["small_cover_image"]=> string(96) "https://yts.mx/assets/images/movies/a_chinese_odyssey_part_one_pandoras_box_1995/small-cover.jpg" ["medium_cover_image"]=> string(97) "https://yts.mx/assets/images/movies/a_chinese_odyssey_part_one_pandoras_box_1995/medium-cover.jpg" ["large_cover_image"]=> string(96) "https://yts.mx/assets/images/movies/a_chinese_odyssey_part_one_pandoras_box_1995/large-cover.jpg" ["state"]=> string(2) "ok" ["torrents"]=> array(2) { [0]=> object(stdClass)#4 (10) { ["url"]=> string(72) "https://yts.mx/torrent/download/0575F561D71DD1961F01FC2CBBE22AF5598A6CF1" ["hash"]=> string(40) "0575F561D71DD1961F01FC2CBBE22AF5598A6CF1" ["quality"]=> string(4) "720p" ["type"]=> string(6) "bluray" ["seeds"]=> int(0) ["peers"]=> int(0) ["size"]=> string(9) "810.78 MB" ["size_bytes"]=> int(850164449) ["date_uploaded"]=> string(19) "2022-04-20 20:07:01" ["date_uploaded_unix"]=> int(1650478021) } [1]=> object(stdClass)#5 (10) { ["url"]=> string(72) "https://yts.mx/torrent/download/633A2CAE8DE3C9F50B2E3B89A5BC6304E4770BFE" ["hash"]=> string(40) "633A2CAE8DE3C9F50B2E3B89A5BC6304E4770BFE" ["quality"]=> string(5) "1080p" ["type"]=> string(6) "bluray" ["seeds"]=> int(0) ["peers"]=> int(0) ["size"]=> string(7) "1.63 GB" ["size_bytes"]=> int(1750199173) ["date_uploaded"]=> string(19) "2022-04-20 21:16:40" ["date_uploaded_unix"]=> int(1650482200) } } ["date_uploaded"]=> string(19) "2022-04-20 20:07:01" ["date_uploaded_unix"]=> int(1650478021) } } } ["@meta"]=> object(stdClass)#6 (4) { ["server_time"]=> int(1650488761) ["server_timezone"]=> string(3) "CET" ["api_version"]=> int(2) ["execution_time"]=> string(4) "0 ms" } } Query was successful
Code that was working using file_get_content(), but knew to fail sometimes and so I was told to use curl.
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;
}
}
Current code
<?php
$url = "https://yts.mx/api/v2/list_movies.json?limit=1";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if($e = curl_error($curl)) {
throw new Exception("Curl request failed: " . $e);
}
curl_close($curl);
$data = json_decode($response);
var_dump($data);
if (!$data) {
throw new Exception("JSON decode error: " . json_last_error_msg());
}
if ($data->status != 'ok') {
throw new Exception("API request failed. Error was: " . $data->status_message);
}
return $data->data;