Facebook Graph Api获取链接帖子的图像

时间:2015-07-06 09:01:18

标签: php facebook facebook-graph-api

我创建了一个简单的小部件来显示Facebook页面中的一个帖子。这是代码:

//VARS
    $title = $instance['title'];        
    $app_id = $instance['add_id'];
    $app_secret = $instance['app_secret'];
    $page_id = $instance['pade_id'];
    $my_url = $instance['my_url'];
    $page_name = $instance['page_name'];
    $message_lenght = $instance['message_lenght'];

    //CONFIGS
    // get user access_token
    $token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
      . $app_id . '&redirect_uri=' . urlencode($my_url) 
      . '&client_secret=' . $app_secret 
      . '&grant_type=client_credentials';

    // response is of the format "access_token=AAAC..."
    $access_token = substr(file_get_contents($token_url), 13);

    $fql_query_url = 'https://graph.facebook.com/v2.3/' . $page_id
      . '/posts?access_token=' . $access_token;
    $fql_query_result = file_get_contents($fql_query_url);
    $fql_query_obj = json_decode($fql_query_result, true);
    $data = $fql_query_obj['data'][0];

    $img_query_url = 'https://graph.facebook.com/v2.3/' . $data['object_id'] . '/?access_token=' . $access_token;
    $img_query_result = file_get_contents($img_query_url);
    $img_query_obj = json_decode($img_query_result, true);
    $msg = substr($data['message'], 0, $message_lenght). '... ';

    if($img_query_obj['source'] == ""){
        $img_url = $data[picture];
    }else{
        $img_url = $img_query_obj['source'];
    }


    //START APP
    echo $before_widget;

    if($title !== ""){
        echo '<h3>'.$title.'</h3>';
    }   
    ?>
    <div class="facebook-widget">
        <pre class="hide"><?php var_dump($data[picture]); ?></pre>
        <div class="img"><img src="<?php echo $img_url; ?>"></div>
        <div class="text">
            <div class="date">
                FACEBOOK / 
                <?php echo date("m.d.Y", strtotime($data['updated_time'])); ?>
            </div>
            <a class="page_title" target="_blank" href="<?php echo $my_url; ?>">/<?php echo $page_name; ?></a>
            <p><?php echo mb_substr($msg, 0, 95); ?>[...]<a href="<?php echo $data[link]; ?>" target="_blank"> (read more)</a></p>                        
        </div>
        <div class="facebookfollow">
            <a class="facebook-fallow-button" href="<?php echo $my_url; ?>" target="_blank">Follow Us</a>
        </div>
    </div>

    <?php
    echo $after_widget;

正如您所见,我添加了条件语句以检查是否有帖子照片。 $ img_query_obj [&#39;来源&#39;] 适用于以正常方式发布的帖子 - 一些文字+图片上传,第二个 - 用于链接帖子 - 从数据查询中获取130x130图片,因为没有图片以标准方式。

所以我的问题是,如果响应中只有130x130的图像,如何获得后期图像?

问候

1 个答案:

答案 0 :(得分:1)

我看到两种方式..它们都不是完美的..

  1. &#34;图片&#34;链接w = 130&amp; h = 130&amp; d = ASDDSA ...你有&amp; url = http://some.urlencoded.link.to.picture.jpg - 所以你可以下载它并在旅游服务器上调整大小..

  2. 忽略&#34;图片&#34;得到&#34;链接&#34; - 每个链接(http://some.page/article)都是图表对象

  3. https://graph.facebook.com/v2.3/http%3A%2F%2Fstackoverflow.com%2F?access_token=YOUR_TOKEN_HERE

    {
       "og_object": {
          "id": "10150180465825637",
          "description": "Q&A for professional and enthusiast programmers",
          "title": "Stack Overflow",
          "type": "website",
          "updated_time": "2015-07-10T22:26:23+0000",
          "url": "http://stackoverflow.com/"
       },
       "share": {
          "comment_count": 11,
          "share_count": 31967
       },
       "id": "http://stackoverflow.com/"
    }
    

    作为回应,您将获得og_objet.id,现在您可以尝试:

    https://graph.facebook.com/v2.3/10150180465825637?access_token=YOUR_TOKEN_HERE

    {
       "created_time": "2008-04-19T03:48:51+0000",
       "title": "Stack Overflow",
       "type": "website",
       "description": "Q&A for professional and enthusiast programmers",
       "image": [
          {
             "height": 316,
             "url": "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon\u00402.png?v=ea71a5211a91&a",
             "width": 316
          }
       ],
       "is_scraped": true,
       "updated_time": "2015-07-10T22:26:23+0000",
       "url": "http://stackoverflow.com/",
       "id": "10150180465825637"
    }
    

    作为回应,您将拥有图片网址,您可以获取并调整大小..