0

I am trying to build a simple script that I can pass in a list of Twitter username and it will then access the Twitter API and returns a list of details for the user's that I requested.

Below is what I have so far, it returns a JSON response with all the data for the 3 user's that I sent in the URL.

I need to figure out how to access each of these items, I plan to save them to a database so I need to be able to access the returned items as a variable. Also the number of users/results in the JSON will be different each time depending on how many names I request so I need to somehow iterate over this JSON response.

Can anyone help me?

$json = file_get_contents('http://api.twitter.com/1/users/lookup.json?screen_name=fishriver,metinogtem,friendproject');

$obj = json_decode($json);
echo '<pre>';
print_r($obj);
echo '</pre>';

2 Answers 2

4

Iterating is easy:

foreach ($obj as $varName => $varValue)
{
   // ...
}
Sign up to request clarification or add additional context in comments.

Comments

3

Use foreach to iterate over the JSON object.

    $json = file_get_contents('http://api.twitter.com/1/users/lookup.json?screen_name=fishriver,metinogtem,friendproject');
    $obj = json_decode($json);
    echo '<pre>';
    foreach($obj as $index => $user) {
        echo $user->screen_name."<br>";
        // insert into database here
    }
    echo '</pre>';

1 Comment

Great now combine this answer in case your json is jsonp stackoverflow.com/questions/2101316/jsonp-json-decode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.