Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8066871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:07:32+00:00 2026-06-05T12:07:32+00:00

I am trying to get the text and username for the most recent tweet

  • 0

I am trying to get the text and username for the most recent tweet using a certain hashtag (in this example, #stackoverflow) . The error returned is Invalid argument supplied for foreach(). I assume this is because $content isn’t actually an associative array.

function get_twitter($hashtag) {

    $json_url = "http://search.twitter.com/search.json?q=%23" . $hashtag . "&rpp=1&result_type=recent";
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$returned_content = get_twitter('stackoverflow');
$content = json_decode($returned_content);
foreach ($content['results'] as $tweet) {
    echo $tweet['text'];
    echo $tweet['from_user'];
}

An example of the JSON that Twitter returns is as follows:

{
"completed_in":0.099,   
"max_id":210895419549548544, 
"max_id_str":"210895419549548544", 
"next_page":"?page=2&max_id=210895419549548544&q=%23stackoverflow&rpp=1&result_type=recent", 
"page":1, 
"query":"%23stackoverflow", 
"refresh_url":"?since_id=210895419549548544&q=%23stackoverflow&result_type=recent", 
"results":[ 
    { 
        "created_at":"Fri, 08 Jun 2012 00:46:00 +0000", 
        "from_user":"Beseeker", 
        "from_user_id":334048944, 
        "from_user_id_str":"334048944", 
        "from_user_name":"Italo Iv\u00e1n", 
        "geo":null, 
        "id":210895419549548544, 
        "id_str":"210895419549548544", 
        "iso_language_code":"es", 
        "metadata":{"result_type":"recent"}, 
        "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1611497833\/imagen-perfil-fb_normal.jpg", 
        "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1611497833\/imagen-perfil-fb_normal.jpg", 
        "source":"<a href="http:\/\/twitter.com\/">web<\/a>", 
        "text":"podr\u00eda pasar horas y horas en #StackoverFlow", 
        "to_user":null, 
        "to_user_id":0, 
        "to_user_id_str":"0", 
        "to_user_name":null
    }
], 
    "results_per_page":1, 
    "since_id":0, 
    "since_id_str":"0"
}

Thanks in advance for any thoughts!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:07:36+00:00Added an answer on June 5, 2026 at 12:07 pm
    function getTweets($hash_tag) {
    
        $url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag) ;
        echo "<p>Connecting to <strong>$url</strong> ...</p>";
        $ch = curl_init($url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $xml = curl_exec ($ch);
        curl_close ($ch);
    
        //If you want to see the response from Twitter, uncomment this next part out:
        //echo "<p>Response:</p>";
        //echo "<pre>".htmlspecialchars($xml)."</pre>";
    
        $affected = 0;
        $twelement = new SimpleXMLElement($xml);
        foreach ($twelement->entry as $entry) {
            $text = trim($entry->title);
            $author = trim($entry->author->name);
            $time = strtotime($entry->published);
            $id = $entry->id;
            echo "<p>Tweet from ".$author.": <strong>".$text."</strong>  <em>Posted ".date('n/j/y g:i a',$time)."</em></p>";
        }
    
        return true ;
    }
    

    Use like this:

    getTweets('#yankees');
    

    Documentation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get edit text to behave in this way: User can input
Using the following code snippets, I'm trying to get the text value which has
I am trying to get text input from the keyboard in Java 6. I
I am trying to get the text to scroll at the same speed as
I'm trying to get the text from a textbox as a user types, so
I am trying to get a text block to resize vertically and use the
I am trying to get the text selection within a an html element and
I'm trying to get the plain text from a word document. Specifically, the xpath
I've trying to get some vertical text on a little test webpage after reading
I am trying to get a input text area and a submit button attached

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.