CURL and JSON problem
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 th开发者_StackOverflow中文版e 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.
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.
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)
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
精彩评论