0

I am trying to get the tweets of a person through codebird-php This is my code:

<?php


require_once('src/codebird.php');
  \Codebird\Codebird::setConsumerKey('Consumer Key', 'Consumer Secret');

  $cb = \Codebird\Codebird::getInstance();
  $cb->setToken('Oauth Key', 'Oauth Secret');
  $params = array(
    'screen_name' => 'WWE',
    'count' => 2
  );
  $reply = $cb->statuses_userTimeline($params);


  json_decode($reply);
  $main = $reply[0]->text;
  echo '<pre>';
  var_dump($reply);
  echo '</pre>';

And Some Of The Output Of the var_dump($reply) is

object(stdClass)#35 (4) {
  [0]=>
  object(stdClass)#2 (23) {
    ["created_at"]=>
    string(30) "Mon Jan 26 08:30:10 +0000 2015"
    ["id"]=>
    float(5.5962935475884E+17)
    ["id_str"]=>
    string(18) "559629354758844416"
    ["text"]=>
    string(112) "RT @WWENXT: .@WWERomanReigns has just made history by becoming the FIRST #WWENXT alumnus to win the #RoyalRumble"
    ["source"]=>
    string(63) "Hootsuite"
    ["truncated"]=>
    bool(false)
    ["in_reply_to_status_id"]=>
    NULL
    ..........

But The json_decode($reply) returns Warning: json_decode() expects parameter 1 to be string, object given Any Help Will Be Very Much Appreciated...
Thanks. Cheers

TecBrat
  • 3,643
  • 3
  • 28
  • 45
Ikari
  • 3,176
  • 3
  • 29
  • 34
  • 3
    you don't need to decode it anymore, its not a JSON string, just access the object properties as it is. – Kevin Jan 26 '15 at 09:07
  • 1
    It's pretty clear and by your example as well that `$reply` is not a json string, but an object – Royal Bg Jan 26 '15 at 09:08
  • Thanks! OK! But When I'm Trying To use this `$main = $reply[0]->text;` It Shows `Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\twitter\codebird-php-develop\twitter.php on line 14`. Is the Code of `$main` is wrong. – Ikari Jan 26 '15 at 09:11
  • `object(stdClass)#35 (4) { [0]=> object(stdClass)#2 (23) {` I see no array there. Only a property named `0`. – Royal Bg Jan 26 '15 at 09:20

2 Answers2

0

statuses_userTimeline($params) is returning the object. Use the object as-is.

TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • Thanks! But When I'm Trying To use this `$main = $reply[0]->text;` It Shows `Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\twitter\codebird-php-develop\twitter.php on line 14`. Is the Code of `$main` is wrong. – Ikari Jan 26 '15 at 09:12
  • So, try `$main = $reply->0->text;` – TecBrat Jan 26 '15 at 09:15
  • Thanks! But Now it is showing `Parse error: syntax error, unexpected '0' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\xampp\htdocs\twitter\codebird-php-develop\twitter.php on line 14` – Ikari Jan 26 '15 at 09:18
  • I'll admit I'm guessing right now, but sometimes I guess right. Try `$main = $reply->{0}->text;` or `$main = $reply->'0'->text;` I'm a little underskilled in object notation, but It's got to be something along these lines, I think. – TecBrat Jan 26 '15 at 09:20
  • Thanks For Those Hints but i tried both and it is showing `Notice: Undefined property: stdClass::$0 in C:\xampp\htdocs\twitter\codebird-php-develop\twitter.php on line 14` – Ikari Jan 26 '15 at 09:26
  • According to [this answer](http://stackoverflow.com/questions/9606340/get-a-php-object-property-that-is-a-number), It's a combination of my last ideas: `$main = $reply->{'0'}->text;` – TecBrat Jan 26 '15 at 09:32
0

I just read The Documentation and Also some Stackoverflow Questions And Found Out That I needed To Change Data Type To array and use it. Then it would look like:

<?php
require_once('src/codebird.php');
  \Codebird\Codebird::setConsumerKey('Consumer Key', 'Consumer Secret');

  $cb = \Codebird\Codebird::getInstance();
  $cb->setToken('Oauth Key', 'Oauth Secret');
  $params = array(
    'screen_name' => 'WWE',
    'count' => 2
  );
  $reply = $cb->statuses_userTimeline($params);
$dat = $data[0];
echo $data[0]->text;
Ikari
  • 3,176
  • 3
  • 29
  • 34