3

I'm using tmhOAuth library to parse twitter stream in php.

It returned me variable $data with this object

object sample for stackoverflow

{
   "created_at":"Wed Jan 02 13:37:54 +0000 2013",
   "id":286466350610776064,
   "id_str":"286466350610776064",
   ...
}

looks like simple object, but how pull some data from this object?

$data->created_at doesn't work, it broke my head, why??

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Denis
  • 2,429
  • 5
  • 33
  • 61
  • 1
    Not to ask the obvious, but isn't it because probably you don't understand that this is Json (http://json.org/) and you need to decode it first otherwise it's a string? http://php.net/json_decode ? – hakre Jan 02 '13 at 13:53
  • move this to answers, it's correct :) thank you so much – Denis Jan 02 '13 at 13:54
  • Done that. However, take a little search next time ;) I could not find a good duplicate so far because it's such a general question, will link it later if I find one. – hakre Jan 02 '13 at 13:57
  • Also consider if you were missing an example with that library to add an example for that library, e.g. suggesting it in an issue: https://github.com/themattharris/tmhOAuth-examples/issues – hakre Jan 02 '13 at 14:06

2 Answers2

4

Isn't it because probably you don't see that this is a string containing Json and you need to decode it first otherwise it's just a string variable and not an object?

See json_decodeDocs:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}
hakre
  • 193,403
  • 52
  • 435
  • 836
2

This is JSON response. Try json_decode

v0d1ch
  • 2,738
  • 1
  • 22
  • 27