How To Read JSON Data Response Using PHP?

Example JSON Response

$json_data = '{
      "stat": "ok",
      "profile": {
        "providerName": "testing",
        "identifier": "http://testing.com/58263223",
        "displayName": "testing 1",
        "preferredUsername": "testing 2",
        "name": {
          "formatted": "testing"
        },
        "url": "http://testing.com/testing/",
        "photo": "https://securecdn.testing.com/uploads/users/5826/3223/avatar32.jpg?1373393837",
        "providerSpecifier": "testing"
      }
    }';


Alternative 1 (returns object format)

$data = json_decode($json_data);
echo $data->profile->displayName; // outputs testing 1
echo $data->profile->preferredUsername;// outputs testing 2

Alternative 2 (returns array format)

$data = json_decode($json_data, true);
$preferredUsername = $data['profile']['preferredUsername'];
$displayName = $data['profile']['displayName'];

Comments

Popular posts from this blog

dynamic create multiple ckeditor onclick event using jquery

get lat and long from city name using jquery

membership registration and login system in php