Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6734283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:52:13+00:00 2026-05-26T10:52:13+00:00

I’ve been having some issues trying to use cURL and JSON to print out

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:52:14+00:00Added an answer on May 26, 2026 at 10:52 am

    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->overview

    I 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.

    $movies = array();
    $movies[] = $movie1;
    $movies[] = $movie2;
    $movies[] = $movie3;
    echo json_encode($movies);
    

    instead of

    echo json_encode($movie1);
    echo json_encode($movie2);
    echo json_encode($movie3);
    

    If you dont have the access to coolection.php you can get individual objects by splitting the output eg:

    $objects = explode('}', $output);
    foreach ($objects as $object){
      $movie = json_decode($object.'}');
      echo "<p>This movie title is: " . $movie->overview . "</p>";
    }
    

    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:

    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");
    $allMovies = array(); // Initializing a variable to hold all movies
    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);
            }
        }
        $allMovies[] = $collection_array; // adding movie to the array of all movies
    }
    echo json_encode($allMovies); // printing json encoded array of all movies
    

    Now you can use your initial script to display results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.