views:

30

answers:

2

I use the following function in CodeIgniter, to get my latest tweet:

function tweet($id) {

        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/".$id.".xml?count=1");

        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

        //Benchmark starts here
        $src = curl_exec($c);
        //Benchmark ends here

        curl_close($c);

        preg_match('/<text>(.*)<\/text>/', $src, $t);
        $data['tweet'] = htmlentities($t[1]);
        preg_match('/<created_at>(.*)<\/created_at>/', $src, $c);
        $created = $c[1];

        // explode $created so we can process it
        $created_array = explode(' ',$created);
        $time = $created_array[3];
        $time_array = explode(':',$time);
        $format = '%b/%d/%Y %H:%M';
        $date_to_format = $created_array[1].'/'.$created_array[2].'/'.$created_array[5].' '.$time_array[0].':'.$time_array[1];
        $date_time = strptime($date_to_format,$format);
        $created_timestamp = mktime($date_time['tm_hour'], $date_time['tm_min'], 0, $date_time['tm_mon']+1, $date_time['tm_mday'], $date_time['tm_year']+1900);
        $time_diff = time() - $created_timestamp;

        $data['time'] = time_since($time_diff);

        return $data;

    }

I use the Benchmark class of CI to see why the website takes so long to respond, and I found that the line

 $src = curl_exec($c);

takes more than 5 seconds to execute. Can anyone tell me why this is happening?

+1  A: 

I just tested your code on my machine (the curl request only) and there isn't any problem... It retrieves the data quickly: 349ms.

Could it be your network that has issues? Or maybe Tweeter had a moment right when you tested your request? Last time I tried to play with the Twitter API, the whole site was down so maybe they had issues on their side as well.

Good luck

Zaziffic
I'm almost certain its not Twitter since its happening for the past 2 days. Googling "curl_exec" I found many having slow execution times with this function. Also its working fast locally (on my MAC), so maybe it has something to do with the server. The only workaround I can think is to make a cron job and store the tweet locally.
Zack
+2  A: 

Try just doing this with file_get_contents.

http://philsturgeon.co.uk/news/2009/07/How-to-Create-a-Twitter-feed-with-full-syntax-support

Phil Sturgeon