6

I have this project I'm working on and id like to add a really small list of nearby places using Facebook's places in an iframe feature from touch.facebook.com. I can easily just use touch.facebook.com/#places.php but then that loads the top bars.

Anyway, is there a way I could manipulate the URL so that it will only load the places content rather than having the top and bottom menu bars. After looking at the code it looks like there is a div id="content". I was thinking maybe there was a way to make the URL so that it will only load that div? I also tried making a URL that might just jump down to content using touch.facebook.com/#places.php#content but apparently with that way touch.facebook.com was built that doesn't work.

I'd really rather figure out a way to just load only the section of content.

I went ahead and tried this approach in PHP. I read the examples for file_get_contents and what's being here looks right, according to example 1:

<?php
$page = file_get_contents('http://touch.facebook.com');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
      if ($div->getAttribute('id') === 'content') {
         echo $div->nodeValue;
    }
}
?>

But, I get a completely blank page.

3

You won't be able to manipulate the URL to get only a portion of the page. So what you'll want to do is grab the page contents via the server-side language of your choice and then parse the HTML. From there you can grab the specific DIV you are looking for and then print that out to your screen. You could also use to remove unwanted content.

With PHP you could use file_get_contents() to read the file you want to parse and then use DOMDocument to parse it and grab the DIV you want.

Here's the basic idea. This is untested but should point you in the right direction:

$page = file_get_contents('http://touch.facebook.com');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
    // Loop through the DIVs looking for one withan id of "content"
    // Then echo out its contents (pardon the pun)
    if ($div->getAttribute('id') === 'content') {
         echo $div->nodeValue;
    }
}
  • I went ahead and tried what you said, but it looks like there is an error with file_get_contents() is there another way it should be used? – brybam Sep 13 '10 at 16:11
  • Your PHP must be set to allow URL fopen (it should be set to 1) – John Conde Sep 13 '10 at 17:31
  • is this something that's defined in my .php document? Or will i have to contact my server host company about my php configuration? Also, I caught a typo I made, and adjusted the first post. Thanks a lot for all the insight on this topic. Like I said, really new to php. – brybam Sep 13 '10 at 18:11
  • Looks like you will need to contact your host as it seems it can only be changed in the php.ini file: php.net/manual/en/… – John Conde Sep 13 '10 at 18:32
  • I've contacted my server company and confirmed the php.ini is configured properly. Now the only issue im having is just a blank page. – brybam Sep 15 '10 at 4:16
0

The page you are trying to access requires authentication. You were probably looking at the data that you wanted after you signed in. That is the reason why when you tried to scrape the page programmatically, it shows the login page's content.

Instead of using this scraping approach, you could check if the info you need can be extracted programmatically using Facebook's APIs

On a related note, you could use YQL to fetch a portion of any webpage on a public site. However in your case, it may not work as the page you are trying to access requires authentication.

  • I have seen it done before. The content div of the main page contains the login area, then after the user is authenticated i can have them redirected to the next page that contains another content div which is all i want to display. I really only have to worry about the authentication once, because i can keep them logged in. I really don't want to go into it programmatically since it's such a small thing im trying to do i figure it wouldnt be worth the time. and ive seen what im trying to figure out done before so i know it can be. – brybam Sep 13 '10 at 13:14
0

Per other replies, I understand that you want to get some beyond authentication page.

Well, first you will need to log in, then access the page you need, and finally process the DOM iteration.

You have curl as an option to handle requests, thus log in. Also you can use curl to get pages content.

http://php.net/manual/en/book.curl.php

http://devzone.zend.com/article/1081

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.