开发者

PHP: Getting url of facebook likes

开发者 https://www.devze.com 2023-04-05 08:47 出处:网络
I am trying to get information on the likes of the page, since I want to order the posts of my site by likes. I decided to do it by scraping the page, and store the likes of each post id in db, then I

I am trying to get information on the likes of the page, since I want to order the posts of my site by likes. I decided to do it by scraping the page, and store the likes of each post id in db, then I can just order using the db. (I can run this 'scraper' every hour or something like that to update the results)

I am doing the following:

 $url = "http://www.mydomain.com";
 $html = file_get_contents($url);
 $dom= new domDocument;
 $dom->loadHTML($html);

And I am getting the following errors:

Notice: DOMDocument::loadHTML() [domdocument.loadhtml]: Namespace prefix fb is not defined in Entity, line: 36 in C:\xampp\htdocs\page.php on line 13

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag fb:like invalid in Entity, line: 36 in C:\xampp\htdocs\page.php on line 13

Somehow the likes are not shown? Also if I do something like this:

 $url = "http://www.mydomain.com";
$html = file_get_contents($url);
echo $html;
preg_match_all(
    '/<div id="LikePluginPagelet".*?>.*?<\/div>/s',
    $html,
    $posts,
    PREG_SET_ORDER
);

print_r($posts)

The first 'echo' actually shows the whole page, but then the print_r just shows Array ( )

Is it possible that some things cannot be grabbed via php? Here is an example page: http://developers.facebook.com/docs/reference/plugins/like/

You c开发者_StackOverflow社区an see that where the like is and the number of likes, the div has id 'LikePluginPagelet', but PHP does not obtain the stuff inside it.

Update:

Can you make code that will get the 500K number of likes from here by scraping the page? http://developers.facebook.com/docs/reference/plugins/like/ Or any other page that contains a number of likes, if I can get that I can use it in my code as well.

Thanks!


You cannot find these elements (like boxes) in the copy of the html that you get with file_get_contents, because these elements are created with javascript. So you would need a final DOM of the page to be able to obtain the counts in this way. With PHP, this is unfortunately not possible.

What you can do, though, is collect all the URLs in question and run them against Facebook API - you'll get the likes very easily, even without the need for any access tokens / etc. The request looks like this:

https://graph.facebook.com/?ids=<URL>


I think you should check out the Simple HTML DOM parser

0

精彩评论

暂无评论...
验证码 换一张
取 消