使用 cURL 通过 PHP 获取 Facebook 相册 JSON

发布于 2024-12-07 03:47:12 字数 1379 浏览 0 评论 0原文

我在《Web Designer》杂志中找到了一个脚本,可以让您从 Facebook 粉丝页面收集专辑数据,并将其放在您的网站上。

该脚本利用 PHP 的 file_get_contents() 函数,该函数在我的个人服务器上运行良好,但在 Network Solutions 托管上不允许。

在查看他们的文档时,他们建议您使用 cURL 会话来收集数据。我以前从未使用过 cURL 会话,因此这对我来说有点神秘。任何帮助将不胜感激。

我“曾经”使用的代码如下所示:

<?php
    $FBid = '239319006081415';
    $FBpage = file_get_contents('https://graph.facebook.com/'.$FBid.'/albums');
    $photoData = json_decode($FBpage);

    $albumID = $photoData->data[0]->id;
$albumURL = "https://graph.facebook.com/".$albumID."/photos";

    $rawAlbumData = file_get_contents("https://graph.facebook.com/".$albumID."/photos");
$photoData2 = json_decode($rawAlbumData);

    $a = 0;

foreach($photoData2->data as $data) {
        $photoArray[$a]["source"] = $data->source;
    $photoArray[$a]["width"] = $data->width;
    $photoArray[$a]["height"] = $data->height;
    $a++;
}
?>

我现在尝试使用的代码如下所示:

<?php
    $FBid = '239319006081415';
$FBUrl = "https://graph.facebook.com/".$FBid."/albums";

    $ch = curl_init($FBUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($ch);
curl_close($ch);
$photoData = json_decode($contents);
?>

但是,当我尝试回显或操作 $photoData 的内容时,很明显它是空的。

有什么想法吗?

I found a script in Web Designer magazine that enables you to gather Album Data from a Facebook Fan Page, and put it on your site.

The script utilizes PHP's file_get_contents() function, which works great on my personal server, but is not allowed on the Network Solutions hosting.

In looking through their documentation, they recommended that you use a cURL session to gather the data. I have never used cURL sessions before, and so this is something of a mystery to me. Any help would be appreciated.

The code I "was" using looked like this:

<?php
    $FBid = '239319006081415';
    $FBpage = file_get_contents('https://graph.facebook.com/'.$FBid.'/albums');
    $photoData = json_decode($FBpage);

    $albumID = $photoData->data[0]->id;
$albumURL = "https://graph.facebook.com/".$albumID."/photos";

    $rawAlbumData = file_get_contents("https://graph.facebook.com/".$albumID."/photos");
$photoData2 = json_decode($rawAlbumData);

    $a = 0;

foreach($photoData2->data as $data) {
        $photoArray[$a]["source"] = $data->source;
    $photoArray[$a]["width"] = $data->width;
    $photoArray[$a]["height"] = $data->height;
    $a++;
}
?>

The code that I am attempting to use now looks like this:

<?php
    $FBid = '239319006081415';
$FBUrl = "https://graph.facebook.com/".$FBid."/albums";

    $ch = curl_init($FBUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($ch);
curl_close($ch);
$photoData = json_decode($contents);
?>

When I try to echo or manipulate the contents of $photoData however, it's clear that it is empty.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

堇年纸鸢 2024-12-14 03:47:12

尝试删除curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);我不太确定它的作用,但我没有使用它,而且我的代码看起来非常相似。我还会使用:

json_decode($contents,true);这应该将结果放入数组而不是对象中。我用这种方法运气更好。

把它放在我的作品类别中。

Try removing curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); I'm not exactly sure what that does but I'm not using it and my code otherwise looks very similar. I'd also use:

json_decode($contents,true); This should put the results in an array instead of an object. I've had better luck with this approach.

Put it in the works for me category.

吖咩 2024-12-14 03:47:12

试试这个,它可以工作

$ch = curl_init($FBUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($ch);
$pageData = json_decode($contents);
//object to array
$objtoarr = get_object_vars($pageData);
curl_close($ch);

Try this it could work

$ch = curl_init($FBUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($ch);
$pageData = json_decode($contents);
//object to array
$objtoarr = get_object_vars($pageData);
curl_close($ch);
时光清浅 2024-12-14 03:47:12

使用 jquery get Json 代替 这个技巧来自 FB 相册下载器 GreaseMonkey 脚本

Use jquery get Json instead This tips is from FB Album downloader GreaseMonkey script

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文