2017-08-04 141 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帮助你:) :) –

相关问题