jennifer
jennifer

Reputation: 301

DOM to parse Facebook wall

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;

Upvotes: 1

Views: 2191

Answers (3)

ifaour
ifaour

Reputation: 38135

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.

Upvotes: 2

Gordon
Gordon

Reputation: 317119

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;
}

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

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().

Upvotes: 2

Related Questions