2017-08-04 139 views
2

我是新來的PHP。我在下面兩個代碼中獲取facebook專輯照片ID並創建時間。但它不工作,不顯示我的結果/ ID,只顯示我的空白頁。如何使用php獲取Facebook相冊照片ID和時間?

這是我的$ json和$ array輸出的兩個代碼。

Code 1;

<?php 
$token="<token>"; 
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token"); 
$json = json_decode($data); 
echo $json->id; 
echo $json->created_time; 
?> 

代碼1個輸出:採用var_dump($json);

object(stdClass)#1 (2) { 
    ["data"]=> 
    array(3) { 
    [0]=> 
    object(stdClass)#2 (2) { 
     ["id"]=> 
     string(15) "160246594547781" 
     ["created_time"]=> 
     string(24) "2017-08-04T18:09:13+0000" 
    } 
    [1]=> 
    object(stdClass)#3 (2) { 
     ["id"]=> 
     string(15) "160246581214449" 
     ["created_time"]=> 
     string(24) "2017-08-04T18:09:12+0000" 
    } 
    [2]=> 
    object(stdClass)#4 (2) { 
     ["id"]=> 
     string(15) "160246587881115" 
     ["created_time"]=> 
     string(24) "2017-08-04T18:09:13+0000" 
    } 
    } 
    ["paging"]=> 
    object(stdClass)#5 (1) { 
    ["cursors"]=> 
    object(stdClass)#6 (2) { 
     ["before"]=> 
     string(20) "MTYwMjQ2NTk0NTQ3Nzgx" 
     ["after"]=> 
     string(20) "MTYwMjQ2NTg3ODgxMTE1" 
    } 
    } 
} 

代碼2:

<?php 
$token="<token>"; 
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token"); 
$array = json_decode($data, true); 
echo $array['data']['id']; 
echo $array['data']['created_time']; 
?> 

代碼2輸出:使用var_dump($array);

array(2) { 
    ["data"]=> 
    array(3) { 
    [0]=> 
    array(2) { 
     ["id"]=> 
     string(15) "160246594547781" 
     ["created_time"]=> 
     string(24) "2017-08-04T18:09:13+0000" 
    } 
    [1]=> 
    array(2) { 
     ["id"]=> 
     string(15) "160246581214449" 
     ["created_time"]=> 
     string(24) "2017-08-04T18:09:12+0000" 
    } 
    [2]=> 
    array(2) { 
     ["id"]=> 
     string(15) "160246587881115" 
     ["created_time"]=> 
     string(24) "2017-08-04T18:09:13+0000" 
    } 
    } 
    ["paging"]=> 
    array(1) { 
    ["cursors"]=> 
    array(2) { 
     ["before"]=> 
     string(20) "MTYwMjQ2NTk0NTQ3Nzgx" 
     ["after"]=> 
     string(20) "MTYwMjQ2NTg3ODgxMTE1" 
    } 
    } 
} 

請幫我解決這個問題。感謝

+0

做'var_dump($ json)'和'var_dump($ array);'在'json_decode()'行後面的代碼1,2中查看輸出結果是什麼?向我們展示 –

+0

我編輯並添加了輸出。 –

回答

0

對於第一個你需要做的: -

<?php 
$token="<token>"; 
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token"); 
$json = json_decode($data); 
foreach($array.data as $arr){ 
    echo $arr.id; echo PHP_EOL; // you can use `echo "<br/>";` 
    echo $arr.created_time;echo PHP_EOL;// you can use `echo "<br/>";` 
} 
?> 

對於第二一個,你可以使用: -

<?php 
$token="<token>"; 
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos? 
fields=id&access_token=$token"); 
$array = json_decode($data, true); 
foreach($array['data'] as $arr){ 
    echo $arr['id']; echo PHP_EOL; // you can use `echo "<br/>";` 
    echo $arr['created_time'];echo PHP_EOL;// you can use `echo "<br/>";` 
} 
?> 

輸出: - https://eval.in/841721

+1

其工作...非常感謝你。 –

+0

@MasudRana請投票答案too.glad幫助你:) :) –

相關問題