DOM to parse Facebook wall

2019-02-20 16:29发布

I am trying to parse messages from a public Facebook fan page wall, but it returns a blank page.

$source = "http://www.facebook.com/?sk=wall&filter=2";
libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
$dom->loadHTML($source);
$xml = simplexml_import_dom($dom);
libxml_use_internal_errors(FALSE);
$message = $xml->xpath("//span[@class='messageBody']");

return (string)$message[0] . PHP_EOL;

3条回答
forever°为你锁心
2楼-- · 2019-02-20 17:08

This is not the right way to fetch data from Facebook, and it's clear that you want to avoid creating a Facebook Application.

But the good news is that you can still use the FQL, try the below query in the Graph API Explorer.

In the below query, we queried the stream table to get the Facebook Developers page's public feeds:

SELECT message
FROM stream
WHERE source_id=19292868552
AND is_hidden = 0
AND filter_key='owner'

It'll return all the "public" feeds of the page. Obviously you may need retrieve more fields to create a meaningful result.


You need to provide a valid access_token to even access public posts. Read more here.

查看更多
姐就是有狂的资本
3楼-- · 2019-02-20 17:11

The DOMDocument::loadHTML() method, which you are using, expects the HTML content as a parameter -- and not an URL.

Here, you are trying to interpret your URL as some HTML content -- and not what it links to.


using this method, you might want to try with one that works on a file, or a remonte content, such as DOMDocument::loadHTMLFile().

查看更多
女痞
4楼-- · 2019-02-20 17:25

Yet another approach would be to use the JSON from the Graph API

$posts = json_decode(
    file_get_contents('https://graph.facebook.com/swagbucks/posts')
);
foreach($posts->data as $post) {
    echo $post->message, PHP_EOL;
}
查看更多
登录 后发表回答