CURL 和 JSON 问题

发布于 2024-11-16 20:49:30 字数 1376 浏览 0 评论 0原文

我正在尝试设置一个脚本来抓取我的 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 技术交流群。

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

发布评论

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

评论(3

生生漫 2024-11-23 20:49:30

基本上,您的 $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 that json_decode() failed is that the output of the curl_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.

牛↙奶布丁 2024-11-23 20:49:30

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

If you look in both of those urls there is no index called data: these are the 2 json responses:

{
   "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"
}

Also make sure that json_decode worked (which could also be an issue)

友谊不毕业 2024-11-23 20:49:30

你得到非对象是因为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

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