0

Hello am trying to update a column in my sql database using values from a foreach loop but am getting..Invalid argument supplied for foreach()

        foreach($ids as $id){
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/
    users/lookup.json?user_id=".$id."");
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // grab URL and pass it to the browser
    $contents = curl_exec ($ch);
    $ok=array();
    $ok=json_decode($contents,true);
    foreach($ok['results'] as $p){
        $location=$p['location'];
        $query=mysql_query("UPDATE tweets SET location=".$location."
         WHERE from_user_id=".$id."");
        if($query){
            print'ok';
        }else{print'sds';}
    }
    // close cURL resource, and free up system resources
    curl_close($ch);
    }

What am i doing wrong?

cheers people!!!!

6
  • 1
    Comment out the foreach and do a print_r($ok['results']); I don't think that it is a valid array. Commented Jul 29, 2012 at 14:51
  • am not getting anything (empty page) Commented Jul 29, 2012 at 14:55
  • Then there is an error. Comment out the entire foreach() loop and put the print_r($ok['results']); directly the json_decode() Commented Jul 29, 2012 at 14:56
  • Then obviously your JSON is not returning a proper array. Commented Jul 29, 2012 at 15:09
  • the problem is with the curl thing because am echoing out the page and I get 1111111 Commented Jul 29, 2012 at 15:18

2 Answers 2

1

This is all you need. It only pulls the last tweet but it is a good start. Your call to twitter api is no longer working, put it in a browser window and you will find that it errors.

$contents = file_get_contents("http://api.twitter.com/users/".$id.".json");
$ok=json_decode($contents,true);

# For Testing Purposes
#echo '<pre>';
#var_dump($ok);
#echo '</pre>';

echo $ok['location'];
Sign up to request clarification or add additional context in comments.

Comments

0

PHP arrays, whether associative or not, have a key and a value for each pair.

So, it should be foreach($ids as $id_number => $id) { as I'm guessing, since you didn't provide more information on the $ids array.

2 Comments

yes there was a problem with the first array but now i want to loop and get the location from this array.
from your other comments, $ok['results'] is not a valid php array

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.