Facebook php sdk v5 - 下载feed的完整图片

时间:2016-01-22 14:37:21

标签: php facebook facebook-graph-api curl

我需要下载(通过php:curl,file_get_contents ...)将完整图片发布到网站文件夹。

我可以获得帖子项目数据,甚至是full_picture网址,但是当我想通过wget或curl下载时,服务器会以403响应。

如果我尝试通过浏览器下载该网址,没问题,我可以看到它。

https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2

但如果我尝试使用curl或wget,我会收到401响应

wget https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2

[1] 9988
[2] 9989

$ converted 'https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457' (ANSI_X3.4-1968) -> 'https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457' (UTF-8)
--2016-01-22 15:20:39--  https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457
Resolving scontent.xx.fbcdn.net (scontent.xx.fbcdn.net)... 31.13.83.4, 31.13.83.4
Connecting to scontent.xx.fbcdn.net (scontent.xx.fbcdn.net)|31.13.83.4|:443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2016-01-22 15:20:39 ERROR 403: Forbidden.

我也试过将访问令牌附加到路径:

...&access_token=XXXXXXXX

这是json帖子项目:

stdClass Object
(
    [id] => 396231587217143_531401453700155
    [created_time] => 2016-01-11T13:21:37+0000
     ...
    [picture] => https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s130x130/12525704_987920591279679_1409522552_n.jpg?oh=34e4a1b419bb842c5f84441f9a781745&oe=574676AE
    [full_picture] => https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2
    [attachments] => stdClass Object
        (
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [media] => stdClass Object
                                (
                                    [image] => stdClass Object
                                        (
                                            [height] => 405
                                            [src] => https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2
                                            [width] => 720
                                        )

                                )

                            [target] => stdClass Object
                                (
                                    [id] => 10153413586308403
                                    [url] => https://www.facebook.com/WorldArchery/videos/10153413586308403/
                                )

                            [type] => video_inline
                            [url] => https://www.facebook.com/WorldArchery/videos/10153413586308403/
                        )

                )

        )

    ...
)

我以这种方式从Facebook页面获取帖子信息。

启动Facebook课程:

$this->facebook = new Facebook([
  'app_id' => $app_id,
  'app_secret' => $app_secret,
  'default_graph_version' => 'v2.5',
  //'default_access_token' => '{access-token}', // optional
]);

获取发布数据:

...
$fields = array(
  'id',
  'application',
  'call_to_action',
  'caption',
  'created_time',
  'description',
  'from',
  'icon',
  'is_hidden',
  'is_published',
  'link',
  'message',
  'message_tags',
  'name',
  'object_id',
  'picture',
  'full_picture',
  'place',
  'privacy',
  'properties',
  'source',
  'status_type',
  'story',
  'story_tags',
  'targeting',
  'to',
  'type',
  'updated_time',
  'with_tags',
  'actions',
  // 'tags',
  // 'object_attachment',
  'feed_targeting',
  'attachments',
);
$data = $this->facebook->get($id . '?fields=' . implode(',', $fields));
$item = json_decode($data->getBody());
$object (object) $item;

1 个答案:

答案 0 :(得分:0)

Ty设置UA就像@CBroe提到的那样:

wget -U Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36 {your-url}

您可以考虑使用cURL作为替代方案: Saving image from PHP URL