How to exctract a page title from a given URL

Hello everyone,
I found a solution to exctract a page title from a URL:

$x = file_get_contents($URL);
preg_match("/<title>(.+?)<\\/title>/", $x, $match);

However, when trying on facebook it didnt work, it returned “Incompatible Browser | Facebook”, is there another way to get this work on every site including FB?

thanks,
ulthane.

see if you get any joy with http://php.net/manual/en/function.get-meta-tags.php instead.

Facebook is not your typical website and it will take a bit more than file_get_contents to extract any data from a page. Both cookies and an AJAX call are involved with opening a page so it’s probably a job for cURL. Failing that Facebook does have a [URL=“http://developers.facebook.com/docs/”]developers API and you may be able to get the information you want through that a whole lot easier.

Thank you both for the answers
but i dont think meta tags is what i need, because meta tag is not always the same as a page tite… (facebook is an example, each page has different titlte but all got the same meta description…)

What developer API do they have that will enable me to get a page title from them?

go into page source and You will find title tag and text between the tag will be the title of that page

What data specifically are you trying to get? If it’s something that can be retrieved using the API’s, use that. If it isnt, you’re violating Facebook’s Terms of Service and we cannot support you in this script. Facebook clearly states that you may not ‘…access Facebook…using automated means (such as harvesting bots, robots, spiders, or scrapers) without our permission.’

I tried searching their API and i couldn’t find it, but i dont know if i search the right way…
I’m simply trying to retrive a specific page title from their site to put in a link tag (like this: <a href=“www.some-facebook.page.com”>the page title</a>)
Each page of facebook has a title given by the creator of the page, which is the best way to describe the link i wanna put…

Issue solved…all i need was adding this line…

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11');

It’s still a violation of Facebook’s ToS.

Yes you’re right, I don’t really get the point but anyway, i’ve changed it so every url that includes fb in it will be named “facebook”
Anyway all pages that do not have public rights are named like this.

@ulthane

Hi, have you thought about trying to contact Facebook for permission? If what you are doing is actually quite benign then they may allow it. To date I have asked for such permission from Coca-Cola, Nautilus Equipment, and Google and in each case they granted me limited rights but enough for what I needed to do. This way you don’t have to worry about it, especially if someone challenges you.

Steve