简体   繁体   English

使用PHP和图形api照片显示来自facebook的图像

[英]Show images from facebook using PHP and graph api photos

I am trying to show my thumbnails of photos from my facebook fan page. 我试图从我的脸书粉丝页面显示我的照片缩略图。 I have studied the facebook dev and graph api. 我研究了facebook dev和graph api。 Now I am trying to using php to automate the process and show the images on my photo page. 现在我正在尝试使用PHP来自动化该过程并在我的照片页面上显示图像。

$facebook_album = file_get_contents("https://graph.facebook.com/10150101465621686/photos");

which returns the following data with the image data repeating for each image (see below): 返回以下数据,每个图像重复图像数据(见下文):

{
   "data": [
      {
         "id": "10150101465701686",
         "from": {
            "name": "Lingua Language Academy",
            "category": "Organization",
            "id": "55206251685"
         },
         "picture": "http://photos-d.ak.fbcdn.net/hphotos-ak-ash4/185883_10150101465701686_55206251685_6497497_7724100_s.jpg",
         "source": "http://a4.sphotos.ak.fbcdn.net/hphotos-ak-ash4/185883_10150101465701686_55206251685_6497497_7724100_n.jpg",
         "height": 540,
         "width": 720,
         "images": [
            {
               "height": 540,
               "width": 720,
               "source": "http://a4.sphotos.ak.fbcdn.net/hphotos-ak-ash4/185883_10150101465701686_55206251685_6497497_7724100_n.jpg"
            },
            {
               "height": 135,
               "width": 180,
               "source": "http://photos-d.ak.fbcdn.net/hphotos-ak-ash4/185883_10150101465701686_55206251685_6497497_7724100_a.jpg"
            },
            {
               "height": 97,
               "width": 130,
               "source": "http://photos-d.ak.fbcdn.net/hphotos-ak-ash4/185883_10150101465701686_55206251685_6497497_7724100_s.jpg"
            },
            {
               "height": 56,
               "width": 75,
               "source": "http://photos-d.ak.fbcdn.net/hphotos-ak-ash4/185883_10150101465701686_55206251685_6497497_7724100_t.jpg"
            }
         ],
         "link": "http://www.facebook.com/photo.php?pid=6497497&id=55206251685",
         "icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yz/r/StEh3RhPvjk.gif",
         "created_time": "2011-02-22T02:41:02+0000",
         "position": 1,
         "updated_time": "2011-02-22T02:41:04+0000"
      },

My questions is... Is there an easy way to put the following info in an array that will be easy to loop through? 我的问题是......是否有一种简单的方法将以下信息放入一个易于循环的数组中?

Something like so that it loops through the data and puts the value of picture in the image src. 这样的东西,它循环数据,并将图片的值放在图像src中。

foreach( $facebook_album as $key => $value){
    echo "<img src='picture'/><br />";
}

Why don't you try json_decode of the json data ? 你为什么不试试json数据的json_decode?

http://php.net/manual/en/function.json-decode.php http://php.net/manual/en/function.json-decode.php

$dataArr = json_decode($dataStr,true); $ dataArr = json_decode($ dataStr,true);

$imageArr = $dataArr["images"]; $ imageArr = $ dataArr [“images”];

This seemed to work for me 这似乎对我有用

$dataArr = json_decode($facebook_album,true);
foreach($dataArr['data'] as $d){    
    echo "<img src=\"".$d['source']."\"><br>";
}

Use following code (php sdk) 使用以下代码(php sdk)

if($user_id){
            // We have a user ID, so probably a logged in user.
            // If not, we'll get an exception, which we handle below.
            try{
                $user_profile = $facebook->api('/me','GET');
                $photos = $facebook->api("/me/photos");

......
...
..
}

Start showing photos one by one 开始逐个显示照片

foreach($photos['data'] as $photo){
                    echo "<p><img src='";
                    echo $photo['images'][0]['source'];
                    echo "'/></p>";
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM