douli1854 2011-09-17 17:50
浏览 20
已采纳

PHP:获取facebook喜欢的网址

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 can 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!

  • 写回答

2条回答 默认 最新

  • dongle3217 2011-09-30 12:00
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟