-3

How to extract the "SALECODE" and "PRICE" from this curl get request in php for later MySql compare and insert if it's different, and what type of JSON is the result?

?php

$url = 'http://everywhere.smartcash.ro/everywhere/rest/TSmartCashMethods/GetArticleInfo/4/1/1/1/';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url); curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json' ));

$result = curl_exec($cURL);

curl_close($cURL);



//print_r($result);

$json = json_decode($result, true); print_r($json);



echo $json["SALECODE"];


?>

Sample Response:

{  
   "result":[  
      {  
         "DATASET":[  
            {  
               "SUPP_UM":"Kg",
               "DETAILS":"70025",
               "PRICE":"1.5",
               "REC":1,
               "CATEG_10":"-",
               "NOTES":"",
               "LASTSUPPLIER":"Furnizor 2",
               "CATEG_9":"-",
               "CATEG_8":"-",
               "CATEG_7":"-",
               "CATEG_6":"-",
               "CATEG_5":"-",
               "CATEG_4":"-",
               "CATEG_3":"-",
               "CATEG_2":"Paine",
               "CATEG_1":"Panificatie",
               "DIVISIBLE":0,
               "SALECODE":"12",
               "VAT_LETTER":"B",
               "ISALT_PRICE":0,
               "VAT_VALUE":"9",
               "LISTED":1,
               "LASTSUPPLY_UNIT_COST":"4",
               "SUPP_UM_RATIO":"3.33",
               "IDEXTAPP":"70025",
               "IDSMARTCASH":1,
               "DESCRIPTION":"12",
               "LASTSUPPLY_DATE":"2015.01.05 00:00:00",
               "SALE_UM":"Buc",
               "IDSMARTCASH_LASTSUPPLIER":2,
               "ALT_UM":"Kg",
               "DISCOUNT":"0",
               "LASTSUPPLY_QTY":"3",
               "LASTSUPPLY_NIR":5,
               "RECVERSION":284,
               "NAME":"PAINE DE SECARA 300G",
               "ITEM_CODES":[  
                  {  
                     "SALECODE":"12",
                     "ITEM":1,
                     "SALECODE_NAME":"PLU"
                  }
               ],
               "SALECODE_NAME":"PLU",
               "ALT_UM_RATIO":"0.3",
               "IDEXTAPP_LASTSUPPLIER":"",
               "CODWITHCRC":"12"
            }
         ]
      }
   ]
}
Brad
  • 159,648
  • 54
  • 349
  • 530
Saky Maky
  • 1
  • 1

2 Answers2

1

Try using $json['result'][0]['DATASET'][0]['SALECODE'] instead.

If you look at your response data closely, you'll see it's heavily nested. result is also an array, so you'll need to choose an item from it. In this case, there's only one. If there are others, you'll need to loop over $json['result'].

Brad
  • 159,648
  • 54
  • 349
  • 530
0
<?php

$url = 'http://everywhere.smartcash.ro/everywhere/rest/TSmartCashMethods/GetArticleInfo/4/1/1/1/';

$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json' ));
$result = curl_exec($cURL);
curl_close($cURL);


$json = json_decode($result, true);

echo "Price: " . $json['result'][0]['DATASET'][0]['PRICE'];
echo "<hr>";
echo "Sales code: " . $json['result'][0]['DATASET'][0]['SALECODE'];

?>
Ilan P
  • 1,602
  • 1
  • 8
  • 13