FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Paring JSON with PHP. Help!





warallthetm
Im using an API to CURL a submitted user image to a remote server, the response is in JSON and I'm not sure how to parse it.

Here is my php CURL file

Code:
<?
$api_key = 'xxxxxx';
$api_secret = 'xxxxx';
$Dirty_uid = $_GET['uid'];
$uid = htmlentities($Dirty_uid);
$Dirty_img = $_GET['img'];
$img = htmlentities($Dirty_img);

$query2 = 'http://rekognition.com/func/api/?api_key='.$api_key.'&api_secret='.$api_secret.'&jobs=face_recognize&urls='.$img.'&name_space=levodou&user_id=levodou';


$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_URL, $query2);    // get the url contents

$data2 = curl_exec($ch2); // execute curl request
curl_close($ch2);

$json2 = json_decode($data2);
print_r($json2)
?>




and here is a sample of the JSON data that comes back (Generic)

Code:

{
  url: "http://farm3.static.flickr.com/2566/3896283279_0209be7a67.jpg",
  face_detection: [
      {
          boundingbox:
          {
            tl: {
              x: 50,
              y: 118
             },
             size:{
              width:232;
              height:232;
             }
          }
          name: "mona_lisa:0.92,barack_obama:0.02,mark_zuckerberg:0.01,"
      }
      ]

  usage: {
    status: "success",
    quota: 999,
    api_id: "xxx"
  }
}


Where the json string says "name" I need my script to only print the username if the ratio (after the Smile is higher then a threshold (lets say .70 for now). How do I do this? I've worked with XML api's before and returning the data was simple with a
Code:

$xml = simplexml_load_string($data);

type thing.



[/code]
thnn
Your call to json_decode can return a associative array if you call it as $json2 = json_decode($data2, true);

This will be useful as you can now navigate through the json data as an array so for example $json2['face_detection']['name'] would return you the string. You could then use explode to seperate it (on the ,) into an array of strings in the format name:value and then you could further use explode on each on of those, this time seperating on the :. This would give you an array where $arr[0] is the name and $arr[1] is the value

So

Code:
$name_str =  $json2['face_detection']['name'];
$name_arr = explode(',', $name_str);
foreach($name_arr as $n)
{
     $name_value_array = explode(':', $n);
     if($name_value_array[1] > 0.7) echo $name_value_array[0];
}


I can't guarantee that the json_decode method has returned that exact PHP array but your call to print_r should help you validate the layout of the array
badai
there is built in function called json_decode in PHP that will convert your JSON to object.

but looking roughly at your JSON, i think there are error in your JSON

some error that can be seen easily:
width & height separated by semi colon instead of comma
height has trailing semi colon (?) or is that semi colon is part of the value?
boundingbox and name is not separated by comma
face_detection and usage is not separated by comma

you can check your JSON here: http://json.bloople.net

once everything is correct, use json_decode to convert it to object. once it become and object, you can access its value directly and check the ratio or whatever condition you have and get the username value.
codersfriend
Do you have curl enabled? it is usually disabled by default. Just check your php.ini Smile
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.