0

I'm not sure how to write the syntax to access "text" in the object that was returned by the twitter standard search API.

I have tried every possible combination of PHP operators that I can think of e.g. $statuses[0]->"text" etc. but I simply just get errors no matter what I do. The only thing that actually printed something to the screen was print_r($statuses).

I have tried using json_decode() but I'm unsure what to do after that. I'm not even sure if that is the correct usage of the function.

stdClass Object
(
    [statuses] => Array
        (
            [0] => stdClass Object
                (
                    [created_at] => Wed Jan 09 01:03:14 +0000 2019
                    [id] => 1082804961115885570
                    [id_str] => 1082804961115885570
                    [text] => Happy Birthday @LeslieAmandaa_ !!

That is some of print_r($statuses). I would like to access [text].

$statuses = $connection->get("search/tweets", array("q" => "birthday"));
echo "<pre>";
print_r($statuses);
echo "</pre>";

That is the PHP I used to

  1. Get the object from the Twitter Standard Search API
  2. Print it to my webpage (what you see in the screenshot)

I expect to be able to echo out just the [text] part but nothing I have tried seems to do that for me (I have looked through other threads and can't find anything that works for my particular object). I would also like to use this object with JavaScript (also jQuery) if anyone has any helpful information on how to pass that data to a JS file.

Ok so I got it to display errors on the page and the error that is likely causing this is:

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in 
C:\inetpub\wwwroot\index.php:22 Stack trace: #0 {main} thrown in C:\inetpub\wwwroot\index.php on line 22
Christopher Bradshaw
  • 2,615
  • 4
  • 24
  • 38

1 Answers1

0

Based on what I'm seeing, I would guess:

$result = $connection->get("search/tweets", array("q" => "birthday"));
print_r($result->statuses[0]->text);

Would be what you're looking for. Seems like the naming of the variable matching the first value in the result is what's leading to the confusion.

As a side note, be careful when referencing values by their index, in the event those values don't exist. Check for them first, or iterate the array.

syntaqx
  • 2,636
  • 23
  • 29
  • Thank you! Is there a way of using this object in JavaScript? – Anonymous Kraken Jan 09 '19 at 02:01
  • You'll need to turn the object into JSON, and then provide it to a JavaScript runtime (such as the browser) - Here's an example of creating your object FROM JSON (which is what the SDK you're using is doing, and then turning it back into JSON: https://www.tehplayground.com/hX2faU1kWA3FMHRK – syntaqx Jan 09 '19 at 02:12