CURL和JSON问题

时间:2011-06-23 14:17:09

标签: php mysql json curl

我正在尝试设置一个脚本来抓取我的facebook页面并将所有信息返回给我,这样我就可以将它插入数据库(Name,Likes等...)。我构建了一个CURL脚本,但由于一些奇怪的原因它不起作用。它引发了我“注意:试图在第26行的C:\ xampp \ XXX \ curltest.php中获取非对象的属性”。

是我的服务器上启用了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);


?>

此外,我想知道如何制作某种“同时”功能。如果,让我们说,我想刮掉10个URL,我不能一个一个地写它们,所以我最好做一个SQL查询从那里提取这些URL。

提前致谢。

3 个答案:

答案 0 :(得分:3)

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

该变量中没有任何内容的原因是因为json_decode()失败了。 json_decode()失败的原因是curl_multi_exec()函数的输出是cURL过程的句柄。如果你read the documentation,你会看到。

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

答案 1 :(得分:0)

如果您查看这些网址的两个,则没有索引名为data:这些是2个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有效(这也可能是一个问题)

答案 2 :(得分:0)

你有非对象因为curl_multi_exec得到了非内容或者内容无法从json转换为对象。你最好在调用$ Likes-&gt; data之前尝试if($ likes)。

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