2

Possible Duplicate:
Parse Json in php

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

<?php
$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=xxx&user_id=xxx';


$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 number (after the : 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.

Community
  • 1
  • 1

1 Answers1

0

You already have most of it. You have json_decodeed the result. Now you just need to access the data like

$name = $json2->face_detection[0]->name
Mike Brant
  • 70,514
  • 10
  • 99
  • 103