Motyar

Freelance Web Developer

Mar 10, 2013

Parsing JSON API response with php

PHP has a powerful JSON parsing system, you can encode JSON string to Object using json_decode and by using Php Object to Array, object can be converted to array. Lets break this in few steps.

1. Fetching the response from API


$response  = file_get_contents($requestUrl);

2. Decoding the JSON data to Object


$jsonobj  = json_decode($response);

4. Converting object to array.

You can use Php Object to Array function to convert object to array.

$resultArray = object2array($jsonobj);

Final code

You can use Php Object to Array function to convert object to array.

$requestUrl = 'https://graph.facebook.com/motyar';
$response  = file_get_contents($requestUrl);
$jsonobj  = json_decode($response);
$resultArray = object2array($jsonobj);
print_r($resultArray);

Labels: ,


By :