Twitter feed on your website with cURL and PHP

Posted by Danny Herran on Aug 15, 2010 in Backend | 3 comments

Pulling code from here and there, I gathered enough information to create a small function that allow you to pull your Twitter feed and display it in your website. What you do is basically call the function and pass a couple parameters. The styling can be done with CSS.

function pull_twitter_feeds($username, $limit=4){
  $url = ''.$username.'&rpp='.$limit;

  // Set up and execute the curl process
  $curl_handle = curl_init();
  curl_setopt($curl_handle, CURLOPT_URL, "$url");
  curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
  curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  $buffer = curl_exec($curl_handle);
  preg_match_all('/<content type=\"html\">(.+?)<\/content>/is', $buffer, $matches, PREG_PATTERN_ORDER);

  foreach($matches[1] as $key => $value){
    $tweets[] = html_entity_decode($value);

  return $tweets;

How to use? Call the function from within your PHP script and then fetch the returned array. The following is a good example of what I am using on

<h2>My Twitter Feed</h2>
<div class="twitter_feed">
      $tweets = pull_twitter_feeds('dannyherran', 5);
        echo '<ul class="tweets">';
        foreach($tweets as $key => $value){
          echo '<li>'.$value.'</li>';
        echo '</ul>';