0

I am getting a response using Unirest library, i need to separate the data, so that based on that data i can call my next query. Here is full json response i am getting while using Unirest library

echo '<pre>'; print_r($response->raw_body); echo '</pre>';

    {
  "status": "success",
  "images": [
    "http://www.example.com/12.jpg"
  ],
  "photos": [
    {
      "url": "http://www.example.com/12.jpg",
      "width": 205,
      "tags": [
        {
          "confidence": 0.978945010372561,
          "center": {
            "y": 64,
            "x": 129
          },
          "height": 79,
          "width": 79,
          "tid": "31337",
          "attributes": [
            {
              "smile_rating": 0.56,
              "smiling": true,
              "confidence": 0.56
            }
          ],
          "uids": [
            {
              "confidence": 0.35399999999999998,
              "prediction": "SE2",
              "uid": "SE2@SEA1"
            },
            {
              "confidence": 0.28999999999999998,
              "prediction": "SE1",
              "uid": "SE1@SEA1"
            },
            {
              "confidence": 0.16,
              "prediction": "Star1",
              "uid": "Star1@SEA1"
            },
            {
              "confidence": 0.106,
              "prediction": "SE3",
              "uid": "SE3@SEA1"
            },
            {
              "confidence": 0.037999999999999999,
              "prediction": "SE6",
              "uid": "SE6@SEA1"
            },
            {
              "confidence": 0.035000000000000003,
              "prediction": "SE5",
              "uid": "SE5@SEA1"
            },
            {
              "confidence": 0.017999999999999999,
              "prediction": "SE4",
              "uid": "SE4@SEA1"
            }
          ]
        }
      ],
      "height": 206
    }
  ]
}

What i am trying is to print like this

Confidence : 0.35399999999999998
Similar: Test2
Mark
  • 111
  • 8

2 Answers2

0

Well, provided it is a valid JSON string, just use a simple json_decode() with true flag so that it returns an array. Example:

$data = json_decode($response->raw_body, true);
$i = 0;
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
    if($i == 5) break;
    echo 'Confidence: ' . $value['confidence'] . '<br/>';
    echo 'Similar: ' . $value['prediction'] . '<br/><br/>';
    $i++;
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Undefined index: uids & Invalid argument supplied for foreach() – Mark Aug 19 '14 at 07:13
  • @Zack i made a revision check again accordingly – Kevin Aug 19 '14 at 07:14
  • Can you example what this line is doing $data['photos'][0]['tags'][0]['uids'] for future learning – Mark Aug 19 '14 at 07:23
  • @Zack basically what happened is from that JSON string, it is converted into a PHP array. since you want the `uids` values, you need to go to that index which is `$data['photos'][0]['tags'][0]['uids']`. and then foreach content of that array loop and print. thats about it – Kevin Aug 19 '14 at 07:29
  • Can i limit the number of results in foreach, i want first 5 – Mark Aug 19 '14 at 07:51
  • @Zack just add a counter if its 5 then break the loop – Kevin Aug 19 '14 at 07:58
  • Done with that, one last question before you become rude to me :) Is there any possibility that i echo results with out foreach, i want to query db for image based on similarity result, confuse how i out mysql query in foreach or somewhere out of for each – Mark Aug 19 '14 at 08:09
  • @Zack no reason for me to be rude. don't try to hardcode each 5 values, just use the mysql query inside the foreach loop, anyway its getting off topic and broad. – Kevin Aug 19 '14 at 08:15
0

The release of Unirest 2.0 had many improvements including ability to set custom JSON decode flags

this gives you more control over the response body type parsing method (json_decode)

Disclaimer: I'm the author of unirest-php and I work at Mashape.

Ahmad Nassri
  • 1,299
  • 1
  • 12
  • 18