CURL and JSON problem


CURL and JSON problem

我正在尝试设置一个脚本,该脚本将抓取我的facebook页面并返回给我所有信息,以便我能够将其插入数据库(名称、点赞等)。我构建了一个CURL脚本,但由于一些奇怪的原因,它不起作用。它告诉我"注意:试图在C:''examplep''XXX''curltest.php的第26行获取非对象的属性"。

是的,我的服务器上启用了JSON和CURL。如果有人愿意帮忙,我会很高兴的

    <?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://graph.facebook.com/19292868552");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_URL, "http://graph.facebook.com/youtube");
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$running=null;
//execute the handles
do {
    usleep(10000);
    $Likes = json_decode(curl_multi_exec($mh,$running));
    return $Likes->data;
          //output the message body
          echo($Likes->likes);
          //add a line break to separate comments
          echo("<br />");   
} while ($running > 0);

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

?>

此外,我想知道如何制作某种"while"函数。比方说,如果我想抓取10个我无法逐一写入的URL,那么我最好做一个SQL查询来从中提取这些URL。

提前谢谢。

基本上,$Likes变量中没有任何内容。您应该进行测试以确保执行和解码成功。

该变量中没有任何内容的原因是json_decode()失败。json_decode()失败的原因是curl_multi_exec()函数的输出是cURL进程的句柄。如果你阅读了文档,你会发现这一点。

您需要使用curl_multi_getcontent()来获取返回的数据。

如果您查看这两个URL,则有没有索引称为data:这是两个json响应:

{
   "id": "19292868552",
   "name": "Facebook Platform",
   "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/211033_19292868552_7506301_s.jpg",
   "link": "http://www.facebook.com/platform",
   "likes": 2158804,
   "category": "Product/service",
   "website": "http://developers.facebook.com",
   "username": "platform",
   "founded": "May 2007",
   "company_overview": "Facebook Platform enables anyone to build social apps on Facebook and the web.",
   "mission": "To make the web more open and social."
}
{
   "id": "7270241753",
   "name": "YouTube",
   "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/41601_7270241753_5799_s.jpg",
   "link": "http://www.facebook.com/youtube",
   "likes": 40013301,
   "category": "Product/service",
   "website": "http://www.facebook.com/youtube",
   "username": "youtube",
   "founded": "2005",
   "company_overview": "YouTube is the the largest online video destination in the world and the third most visited Website overall. The site exceeds two billion views a day - nearly double the prime time audience of all three major U.S. networks combined. The platform comprises the largest video-sharing community in the world and includes users, advertisers and over 10,000 partners. Every minute 24 hours of video uploaded to the site. Hundreds of millions of users spanning the globe come to YouTube to discover and shape the world through video.",
   "mission": "To be the best place on earth to create, watch, discover and share videos.",
   "products": "More to explore at: http://www.youtube.com/testtube",
   "description": "YouTube - We  | Facebook"
}

还要确保json_decode工作(这也可能是一个问题)

因为curl_multi_exec得到了非内容,或者内容无法从json转换为对象,所以得到了非对象。在调用$likes->data之前,最好先尝试if($likes)。

您可以编写一个函数来处理单个调用,但不使用curl_multi-