0

Afternoon,

I am using PHP Zend to search twitter API in order to find tweets with links, I can get the URL in the tweets using the entities array but I need to display the page title of the URL.

I am using DOMDocument() the following code below to get the title, is there any other way to get the URL title because it take an age for the page to load due to the pinging all the URL's or is there some info in the twitter response I am missing?

function getTitle($Url){
        $urlContents = file_get_contents($Url);
        $dom = new DOMDocument();
        @$dom->loadHTML($urlContents);

        $title = $dom->getElementsByTagName('title');

        return $title->item(0)->nodeValue; // "Example Web Page"            
    }

Thanks in advanced!

J.

John Jones
  • 2,175
  • 2
  • 20
  • 29
  • 1
    Not sure about which is faster, but take a look here: http://stackoverflow.com/questions/4348912/get-title-of-website-via-link – Keyne Viana Feb 12 '12 at 03:06
  • That second bit of code doesnt work, there must be a way to do this tweetmeme.com can achieve it. – John Jones Feb 12 '12 at 14:21

2 Answers2

0

because it take an age for the page to load due to the pinging all the URL's

So you're loading all tweets upon a page view and then resolving all links? Yes, that'll take a while. You should store the URL's and their titles somewhere, say in a database, so you can quickly look them up again.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Excuse my ignorance but how would I do this? Grab the titles on inital load and then store tweet id, title and URL and when page loads again check if title stored in db by using tweet id? – John Jones Feb 13 '12 at 15:50
0

If the following is indicative of the results you are working with.

10.    {
11.      "created_at":"Thu, 06 Oct 2011 19:36:17 +0000",
12.      "entities":{
13.        "urls":[
14.          {
15.            "url":"http://t.co/L9JXJ2ee",
16.            "expanded_url":"http://bit.ly/q9fyz9",
17.            "display_url":"bit.ly/q9fyz9",
18.            "indices":[
19.              37,
20.              57
21.            ]
22.          }
23.        ]
24.      }

Then it looks like some version of what you're already doing is what you need. You can cache it either in a MySQL database or a flat-file. If you're not familiar with either of those, make Google your friend. Cache won't help initial load time, but will help the next time someone accesses the page

You might consider letting the page load without titles, then fill them in with an AJAX call.

TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • TecBrat Yes this is the data I get. But I need the page title any thoughts. ? – John Jones Feb 21 '12 at 19:08
  • Then it looks like some version of what you're already doing is what you need. You can cache it either in a MySQL database or a flat-file. If you're not familiar with either of those, make Google your friend. Cache won't help initial load time, but will help the next time someone accesses the page. – TecBrat Feb 22 '12 at 00:07
  • You might consider letting the page load without titles, then fill them in with an AJAX call. – TecBrat Feb 22 '12 at 03:19