0

I'm trying to grab specific values from json data.I make a call to web service and i use this code

<?php

//API Url
$url = 'http://myUrl/s1services';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
 'service' => 'login',    
 'username' => 'demo',
 'password' => 'demo',
 'appid' =>  '256'
);

//Encode the array into JSON.
$jsonData = json_encode($jsonData, JSON_PRETTY_PRINT | JSON_INVALID_UTF8_SUBSTITUTE);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//Set the compression type to gzip 
curl_setopt($ch,CURLOPT_ENCODING , "gzip");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch,CURLOPT_ENCODING , "utf-8");

//execute post
$result = curl_exec($ch);
curl_close($ch);

echo $result;



?>

Response i get in json below(I don't know why some fields return in binary format)

> {    "success":true,   
> "clientID":"9J8pJsP8KKnwH69V9JL4HNLKQrbpINb4SbDVOKPPG6X2JKbvLrLoM65ATqXLKYKrH2KtH5LLTaL5KdP0K4zPLNLOK2KrH5HGL6L4",
> "objs":[
>       {
>          "COMPANY":"1000",
>          "COMPANYNAME":"������� Demo ��",
>          "BRANCH":"1000",
>          "BRANCHNAME":"����� - ����",
>          "MODULE":"13",
>          "MODULENAME":"�������",
>          "REFID":"47",
>          "REFIDNAME":"����������� AE",
>          "USERID":"1",
>          "FINALDATE":"",
>          "ROLES":"",
>          "XSECURITY":"0",
>          "EXPTIME":""
>       }    ],    "ver":"5.00.520.11321",    "sn":"01100313514211",    "off":false,    "pin":false,    "appid":"256" }

I want to grab clientID and COMPANYNAME values.

How can i do this ?

Thanks for any help

jeriko
  • 5
  • 4

1 Answers1

0

By using json_decode().

$result_array = json_decode($result);

echo '<pre>';
print_r($result_array );
echo '</pre>';

echo "Client ID:".$result_array ->clientID;
echo "<br>";
echo "Client ID:".$result_array ->objs[0]->COMPANYNAME;

$result is formatted in json so you need to convert it to array by using json_decode().

so you can easily navigate and use all the keys and values.

I tested your json string on my local PC, enter image description here

And this was my output, it worked well. enter image description here

Jovylle
  • 859
  • 7
  • 17
  • Unfortunately return no values This piece of code i have add `//execute post $result = curl_exec($ch); $result_array = json_decode($result); curl_close($ch); echo $result_array['clientID']; echo $result_array['objs'][0]['COMPANYNAME']; ` – jeriko Jun 15 '20 at 05:27
  • okay, you can try this, and tell use what does shows $result_array = json_decode($result); print_r($result_array ); – Jovylle Jun 15 '20 at 05:44
  • Unfortunately return no values too – jeriko Jun 15 '20 at 08:12
  • if "echo $result" has value, "json_decode($result);" must have a problem – Jovylle Jun 15 '20 at 08:17
  • echo result has value , you are correct! What does it means?What can i do about this ? – jeriko Jun 15 '20 at 11:50
  • can you, paste here the value? – Jovylle Jun 15 '20 at 11:54
  • here it is {"success": true, "clientID":"9J8pHsHKOKzgR59M9JL2RLLKULb3I6WbDKCbDZ1KLNbLGN94OqbOJLHLH4XsHIKtGcnLLYKtGYKtH7LLSNb5LYKtGoKrGt9EKoKrH5KbDKDGGKHKSb55", "objs":[{"COMPANY" : "1000","COMPANYNAME" : "������� Demo ��","BRANCH" : "1000","BRANCHNAME" : "����� - ����","MODULE" : "13","MODULENAME" : "�������","REFID" : "47","REFIDNAME" : "����������� AE","USERID" : "1","FINALDATE" : "","ROLES" : "","XSECURITY" : "0","EXPTIME" : ""}], "ver":"5.00.520.11321", "sn":"01100313514211", "off":false, "pin":false, "appid":"256"} – jeriko Jun 15 '20 at 11:55
  • as i try to convert the sholw json, it gives me error "417 - Expectation Failed" its an error my eyes first encountered. But when i decrease the json. It successfully converts. Im not sure what is happening. – Jovylle Jun 15 '20 at 12:09
  • i have updated my answer, please see it, i have tested the json string on my pc. – Jovylle Jun 15 '20 at 12:33
  • Not working again friend.You have json into php file , i request the json object from remote server.Something is going on there.Ι don't understand what it is! – jeriko Jun 15 '20 at 15:41
  • Jovylle Bermudez, $output = mb_convert_encoding($output, 'UTF-8', 'ISO-8859-7'); solved the problem , and your code was the correct one, thanks for your help – jeriko Jun 19 '20 at 21:12