簡體   English   中英

如何使用PHP獲取Facebook相冊的照片ID和時間?

[英]How to get facebook album photos id and time using php?

我是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"
    }
  }
}

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

對於第一個,您需要做:-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM