CURL 和 JSON 问题
我正在尝试设置一个脚本来抓取我的 Facebook 页面并向我返回所有信息,以便我能够将其插入数据库(姓名、喜欢等)。我构建了一个 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);
?>
我还想知道如何制作某种“while”函数。比方说,如果我想抓取 10 个 URL,我无法将它们一一写入,所以我最好进行 SQL 查询来从那里提取这些 URL。
提前致谢。
I'm trying to set up a script that will scrape my facebook pages and return to me all the info so I will be able to insert it to the database (Name, Likes etc..). I built a CURL script but its not working for some weird reason. It throws me "Notice: Trying to get property of non-object in C:\xampp\XXX\curltest.php on line 26".
Yes JSON and CURL are enabled on my server. I will be glad if someone will help. ;)
<?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);
?>
Also I would like to know how to to make some kind of "while" function. If, let's say, I would like to scrape 10 URLs I can't write them one-by-one so I would be better off making an SQL query to pull those URLs from there.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,您的
$Likes
变量中没有任何内容。您应该进行测试以确保您的执行和解码成功。该变量中没有任何内容的原因是
json_decode()
失败。json_decode()
失败的原因是curl_multi_exec()
函数的输出是 cURL 进程的句柄。如果您阅读文档,您就会看到这一点。您需要使用
curl_multi_getcontent()
获取返回的数据。Basically, there is nothing in your
$Likes
variable. You should test to make sure your exec and decode succeeded.The reason there is nothing in that variable is because
json_decode()
failed. The reason thatjson_decode()
failed is that the output of thecurl_multi_exec()
function is a handle to a cURL process. If you read the documentation, you will see that.You need to use
curl_multi_getcontent()
to get the data returned.如果您查看这两个网址,没有名为
data
的索引:这些是 2 个 json 响应:还要确保
json_decode
有效(这也可能是一个问题)If you look in both of those urls there is no index called
data
: these are the 2 json responses:Also make sure that
json_decode
worked (which could also be an issue)你得到非对象是因为curl_multi_exec得到非内容或者内容无法从json转换为对象。你最好在调用$Likes->data之前尝试if($likes)。
您可以编写一个函数来处理单个调用,但不能使用curl_multi
you got non-object because of curl_multi_exec got non-content or the content can't be convert to object from json.you'd better try if($likes) before call $Likes->data.
you can write a function to process a single call but not using curl_multi