I'am creating a search engine.In this search engine we search movies by actor's name . Searching movies through actor's name returns two different types of JSON data . If the actor name is not found then the JSON is returned in the following format (see the http://netflixroulette.net/api/api.php?actor=)
{"errorcode":404,"message":"Sorry! We couldn't find any movies with that actor!"}
and when the actor name is found the JSON returned is multidimensional and is in the following format.(see the link:http://netflixroulette.net/api/api.php?actor=Yuki%20Kaji)
[{"unit":883,"show_id":70299043,"show_title":"Attack on Titan","release_year":"2013","rating":"4.6","category":"Anime","show_cast":"Yuki Kaji, Yui Ishikawa, Marina Inoue, Daisuke Ono, Hiro Shimono, Hiroshi Kamiya, Keiji Fujiwara, Kish\u00f4 Taniyama, Romi Park, Ryota Ohsaka","director":"","summary":"For over a century, people have been living behind barricades to block out the giant Titans that threaten to destroy the human race. When a Titan destroys his hometown, young Eren Yeager becomes determined to fight back.","poster":"http:\/\/netflixroulette.net\/api\/posters\/70299043.jpg","mediatype":1,"runtime":"24 min"}, { "unit":17256,"show_id":80009097,"show_title":"Magi","release_year":"2012","rating":"3.8","category":"TV Shows","show_cast":"Kaori Ishihara, Erica Mendez, Yuki Kaji, Erik Scott Kimerer, Haruka Tomatsu, Cristina Valenzuela, Daisuke Ono, Matthew Mercer, Takahiro Sakurai, Lucien Dodge","director":"","summary":"A land of mysterious ruins and a magical treasure hunt await young Aladdin and his courageous friend Alibaba for the adventure of their lives.","poster":"http:\/\/netflixroulette.net\/api\/posters\/80009097.jpg","mediatype":0,"runtime":"N\/A"}]
I've tried this code
$output = json_decode($output);
if($output['errorcode']==400)
{
foreach($output as $key =>$row)
{
echo "<p>$key : $row";
echo '<br>';
}
}
else
{
foreach($output as $value)
{
foreach($value as $key =>$row)
{
if($key == "mediatype" || $key == "runtime" || $key == "unit" || $key == "show_id" )
continue;
else if($key == "show_cast" )
{
echo"<br>Show Cast:";
$pieces = explode(",", $row);
foreach($pieces as $strings)
{
$link='http://localhost:8000/?Title=&director=&Actor='.$strings;
echo "<li><a href='$link'>$strings</a>";
}
echo "<br>";
}
else if($key == "director" )
{
if(empty($row))
echo"<br>Director:No details of director<br>";
else
{
echo"<br>Director:";
$link='http://localhost:8000/?Title=&director='.$row.'&Actor=';
echo "<a href='$link'>$row</a><br>";
}
}
else if($key !="poster")
{
echo "$key : $row";
echo '<br>';
}
else
{
echo '<img src="'.$row.'" />';
}
}
echo "<br><br><br><br><br><br>";
}
}
Using this gives me error of "Undefined index: errorcode" . Basically my question is what to use in if-else condition to differentiate between the two received JSON objects that are recieved.
Please help!! Thanks in advance..