I’ve been having some issues trying to use cURL and JSON to print out some JSON results on my page. Here’s my code as it stands just now…
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.wardgraphics.com/moviepickr/collection.php?user=1");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = json_decode(curl_exec($ch));
foreach($output AS $movie) {
// echo the output
echo "<p>This movie title is: " . $movie->overview . "</p>";
}
// close curl resource to free up system resources
curl_close($ch);
When I ouput all i get is:
Warning: Invalid argument supplied for foreach() in /data/26/2/45/90/2371416/user/2602457/htdocs/moviepickr/tester.php on line 15
here’s an example of what’s being outputted by the php file “collection.php?user=1”:
{"title":"Shaun of the Dead","released":"2004-09-24","trailer":"http://www.youtube.com/watch?v=CfBewQPFdKE","runtime":95,"overview":"Shaun of the Dead is a humorous homage to Zombie movies from director Edgar Wright; an outrageous romantic comedy with zombies.","poster":"http://cf1.imgobject.com/posters/089/4e816b465e73d6767f000089/shaun-of-the-dead-cover.jpg"}
connection.php file:
header('Content-type: application/json');
mysql_connect('serv', 'user', 'password');
mysql_select_db('name');
include('databasefile.php');
//'json' is set as default return format
$tmdb = new TMDb('key');
$check_collection = mysql_query("SELECT * FROM collections WHERE user_id = '$_REQUEST[user]' ORDER BY id");
while ($looped = mysql_fetch_assoc($check_collection)) {
$id = $looped[movie_id];
//Search Movie with other return format than the default
$json_movies_result = $tmdb->getMovie($id);
// Convert JSON to array of objects
$movies = json_decode($json_movies_result);
foreach ($movies AS $movie)
{
foreach($movie->posters as $poster)
{
if ($poster->image->size == 'cover') {
$poster_url = $poster->image->url;
}
}
$id = $movie->id;
$json_extra = $tmdb->getMovie($id);
$extra_info = json_decode($json_extra);
foreach($extra_info AS $extra)
{
// convert json results into new php array
$collection_array = array("title" => $movie->original_name, "released" => $movie->released, "trailer" => $extra->trailer, "runtime" => $extra->runtime, "overview" => $movie->overview, "poster" => $poster_url);
}
}
echo json_encode($collection_array);
}
This may be simple for those of you that know your stuff, let me know if it is.
Thanks a ton!
Well the result returned by wardgraphics.com is an object not an array. So you can’t do foreach on it (unless you implement iterator interface). Try printing just
$output->overviewI took a look at your coolection.php output. All you need to do is to put all the movies in the array before you encode it in JSON eg.
instead of
If you dont have the access to coolection.php you can get individual objects by splitting the output eg:
This is not perfect as this method will fail if any data inside the JSON object contains character “}”. So to be safe it is better to use regular expressions here with proper check for escapes.
Here’s amended code of your php file:
Now you can use your initial script to display results.