0

I recently tried using CURL in codeigniter to make a login function using API.

I successfully got the returned value as a JSON.

Here is the JSON response:

[{"isIT":"FALSE","branch_id":"800","login_id":"YohanesED ","login_name":"Yohanes Erwin Dari","Password":"eH3Yw9yLzJDTZ2nKG0QVig==","admin":"0","email_add":"Yohanes.Dari@mpm-finance.com","cc_bo":"1","groupid":"MO","active":1,"webcalender":0,"reservation":0,"fixedasset":0,"dms":1,"its":1,"logoffapproval":0,"useralternate":" ","bballowed":0,"EmployeePositionID":"GMP ","usercc":0,"aduser":null,"posid":"Select One","compliance":1,"admindata":0,"groupidcompliance":"User","areaidmak":"All","ProhelpdeskAdmin":0,"last_upd":"2018-09-21T10:40:10.07","usr_upd":"SunuH ","groupidadd":"000","isPA":null,"isPAR":null,"areaidphdp":null}]

Here is curl setup on my controller:

$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

How do I read specific content on the JSON response?

SherylHohman
  • 16,580
  • 17
  • 88
  • 94

1 Answers1

0

You can Use with json_decode

<?php 

$json = '{"isIT":"FALSE","branch_id":"800","login_id":"YohanesED ","login_name":"Yohanes Erwin Dari","Password":"eH3Yw9yLzJDTZ2nKG0QVig==","admin":"0","email_add":"Yohanes.Dari@mpm-finance.com","cc_bo":"1","groupid":"MO","active":1,"webcalender":0,"reservation":0,"fixedasset":0,"dms":1,"its":1,"logoffapproval":0,"useralternate":" ","bballowed":0,"EmployeePositionID":"GMP ","usercc":0,"aduser":null,"posid":"Select One","compliance":1,"admindata":0,"groupidcompliance":"User","areaidmak":"All","ProhelpdeskAdmin":0,"last_upd":"2018-09-21T10:40:10.07","usr_upd":"SunuH ","groupidadd":"000","isPA":null,"isPAR":null,"areaidphdp":null}';

// Decode JSON data to PHP associative array
$arr = json_decode($json, true);

// Loop through the associative array
foreach($arr as $key=>$value){
    echo $key . " => " . $value . "<br>";
}
?>
Amanjot Kaur
  • 2,028
  • 4
  • 18
  • 33
Jayshri Ghoniya
  • 266
  • 1
  • 9