如何使用PHP获取Facebook专辑照片的ID和时间?

时间:2017-08-04 18:01:02

标签: php facebook facebook-graph-api

我是php的新手。我在下面编写了两个代码来获取facebook相册照片ID并创建时间。但它没有工作,也没有向我显示结果/ ID,只显示我的空白页。

这是我的两个带有$ json和$ array输出的代码。

代码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"
    }
  }
}

请帮我解决这个问题。谢谢

1 个答案:

答案 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