简体   繁体   English

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

[英]Facebook Graph Api get image of linked posts

I've created a simple widget to display one post from facebook page. 我创建了一个简单的小部件来显示Facebook页面上的一个帖子。 Here is code: 这是代码:

//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;

As You can see I added conditional statement to check if there is photo of post. 正如您所看到的,我添加了条件语句来检查是否有帖子的照片。 $img_query_obj['source'] is for posts published normal way - some text + image upload, second - for linked posts - get 130x130 image from data query, because there is no image in standard way. $ img_query_obj ['source']用于以正常方式发布的帖子 - 一些文本+图像上传,第二个 - 用于链接帖子 - 从数据查询中获取130x130图像,因为标准方式没有图像。

So my question is, how to get post image if there is only 130x130 image in response? 所以我的问题是,如果响应只有130x130图像,如何获得后期图像?

Greetings 问候

I see two ways.. none of them is perfect.. 我看到两种方式..它们都不是完美的..

  1. In "picture" link amongst w=130&h=130&d=ASDDSA... you have &url= http://some.urlencoded.link.to.picture.jpg - so you can download it and resize on tour server.. 在“图片”链接w = 130&h = 130&d = ASDDSA ...你有&url = http://some.urlencoded.link.to.picture.jpg - 所以你可以下载它并在旅游服务器上调整大小..

  2. Ignore "picture" and get "link" - each link ( http://some.page/article ) is graph object 忽略“图片”并获取“链接” - 每个链接( http://some.page/article )是图形对象

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/"
}

In response you will get og_objet.id and now you can try: 作为回应,您将获得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"
}

In response you will have image url, you can get and resize.. 作为回应,您将拥有图片网址,您可以获取并调整大小..

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

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