2011-10-27 75 views
1

我一直在嘗試使用cURL和JSON在我的頁面上打印出一些JSON結果。這裏是我的代碼,因爲它剛纔站立...JSON用PHP cURL

// 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); 

當我輸出中我得到的是:

Warning: Invalid argument supplied for foreach() in /data/26/2/45/90/2371416/user/2602457/htdocs/moviepickr/tester.php on line 15

這裏有什麼東西被由php文件輸出的例子「集合?.PHP用戶= 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文件:

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); 

} 

這可能是簡單的爲那些你知道你的東西,讓我知道,如果它是。

謝謝!

+0

這只是你得到的一個結果。要麼刪除該foreach。或者測試結果('is_array'),如果該API可能真的返回一個列表。 – mario

+0

不要將curl結果直接傳遞給json_decode。如果curl中有任何類型的失敗,它將返回一個布爾值,然後解碼,然後嘗試foreach。永遠不要假設捲曲調用成功 - 總是檢查失敗。 –

回答

3

那麼由wardgraphics.com返回的結果是一個對象而不是數組。所以你不能對它進行foreach(除非你實現迭代器接口)。試試打印$output->overview

我看了一下你的coolection.php輸出。所有你需要做的就是把所有的電影放入數組中,然後用JSON編碼它。

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

,而不是

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

如果你沒有訪問到coolection.php您可以通過分割輸出如得到單個對象:

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

這是不完美的,因爲這方法將失敗,如果JSON對象內的任何數據包含字符「}」。所以爲了安全起見,最好在這裏使用正則表達式來檢查轉義。

這裏的修正你的PHP文件的代碼:

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 

現在你可以使用你最初的腳本來顯示結果。

+0

這是我的collection.php數組是這樣的: $ collection_array = array(「title」=> $ movie-> original_name,「released」=> $ movie-> released,「trailer」=> $ extra->預告片,「運行時」=> $ extra-> runtime,「overview」=> $ movie-> overview,「poster」=> $ poster_url); –

+0

@布賴恩這個數組只包含一部電影。但你一次返回幾部電影。因此,您需要先將所有這些影片添加到數組中,然後再使用JSON進行編碼。如果你從collection.php發佈代碼,我將能夠向你展示我的意思。 – Ivan

+0

我已經在上面的原始編輯的文章中發佈了完整的代碼到collection.php。乾杯! –

0

您正在獲取一個PHP解釋爲對象而不是列表的哈希對象。更改collection.php返回

[ 
    { 
     "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" 
    } 
] 

通知的[]

0

如果設置在json_decode爲true的第二個參數,它將返回對象的數組。