Link to home
Start Free TrialLog in
Avatar of Cree
Cree

asked on

Twitter API JSON GEOCODE

I'm grabbing data off of twitter and need help with obtaining geo coordinates (if exist)

currently, what i'm doing is
$l = count($decode->results);
    for ($i=0; $i < $l; $i++) { 
        $dirArray[$i][0] = $decode->results[$i]->text;  
        $dirArray[$i][1] = $decode->results[$i]->from_user; 

Open in new window

I'd like to grab geo also but .. sometimes Geo is Null and other times it's an array.

not null:
"geo":{"coordinates":[33.045491,-96.731439],"type":"Point"},

Open in new window


null:
,"geo":null,

Open in new window


how how should i set up my $dirArray[$i][1] = $decode->results[$i]->
to get geo whether it's null or not?  
I've tried
$dirArray[$i][8] = $decode->results[$i]->geo;
and putting it into a varchar.. but that doesn't work since at times it's  an array.

Any help please!!!
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Without a little more context, it is impossible to give a precise answer, but this strategy may be helpful.  You might want to use isset() to test and see if the geo property contains coorinates.  You can also test with is_null().
http://us1.php.net/manual/en/function.isset.php
http://us1.php.net/manual/en/function.is-null.php

I would recommend using var_dump($decode->results[$i]->geo); to see the exact structure of the variable after it is decoded.
Avatar of Cree
Cree

ASKER

ok.. thanks

I'm a little further

I now have a variable ($j) that contains an array.  when I dump it, it gives me this:

object(stdClass)#252 (2) { ["coordinates"]=> array(2) { [0]=> float(-22.904169) [1]=> float(-43.177224) } ["type"]=> string(5) "Point" } 

Open in new window


If I wanted to assign each array to a variable.. say $y and $z.. how would I go about doing it?
I'd like to give you a code example, but I'm not following this completely, so I can't produce tested code.  It looks like you have an object instance of stdClass that contains some properties, probably including parts of a geocode.  A geocode is a latitude/longitude pair, always expressed in that order, as a pair of signed decimal numbers separated by a comma.

I am guessing that this might be what you want to use (assuming that this describes a location in Rio de Janeiro).

$lat = (string)$obj->coordinates[0];
$lon = (string)$obj->coordinates[1];

Try that and see if it makes sense.
Avatar of Cree

ASKER

It does make sense but it's not working.

The code i entered into the loop is this:

    var_dump($j)   ;
  $lat = (string)$obj->coordinates[0];
$lon = (string)$obj->coordinates[1];
 ECHO '<br> here: '.$lat. '<br>';

what I'm getting when running is:

object(stdClass)#8 (2) { ["coordinates"]=> array(2) { [0]=> float(36.973798) [1]=> float(35.310664) } ["type"]=> string(5) "Point" }
here:
object(stdClass)#11 (2) { ["coordinates"]=> array(2) { [0]=> float(35.243236) [1]=> float(136.985178) } ["type"]=> string(5) "Point" }
here:


So, $lat is not retrieving the array data correctly.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cree

ASKER

That was exactly what I was looking for.
Thanks for the points and thanks for using EE - it's a great question ~Ray