2

I'm using this code:

<?php

$json_url = 'http://search.twitter.com/search.json?q=royal&wedding';

$ch = curl_init( $json_url );

$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);

curl_setopt_array( $ch, $options );

$result =  curl_exec($ch);

if (!empty($result)){
    $json = json_decode($result);
    foreach ($json->results[0]->text as $t){
        var_dump($t);
    }
}

?>

But i'm getting an invalid argument for the foreach??

Any ideas?

1
  • I don't know how json_decode works, but ,at the first glance, it seems you're searching for iterate the text value of the result[0] of your json output. This is wrong. Try to iterate $json results variable and inside the foreach cycle you can read the text value of the current item. Commented May 2, 2011 at 15:00

5 Answers 5

2

Try:

var_dump($json->results[0]->text);

And you'll get something like:

string(166) "RT @womensweard...

which means, you're trying to iterate over a string.

I guess, what you want is:

foreach ($json->results as $t){
  var_dump($t->text);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're looping the text property of the first result. If you want to output the text for each result, you need to loop the results instead:

foreach ($json->results as $t){
    var_dump($t->text);
}

Comments

0

Check that the CURL request succeeded:

$result = curl_exec($ch);

if ($result === FALSE) {
    die(curl_error($ch));
}

Then check that you actually received JSON data:

var_dump($result);

then check if the json_decode succeeded:

$json = json_decode($result);
if(is_null($json)) {
    die(json_last_error());
}

Then check if there actually is a 'results' entry in the json data:

if (!isset($json->results)) {
   die("No results section");
}

Comments

0

Idea: $result is not a valid JSON, and your $result is not an array.

Comments

0

Adding CURLOPT_SSL_VERIFYPEER => false to the CURL options within the performRequest() method in TwitterAPIExchange.php seems to solve it.

cf here

Comments

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.