如何从php中的curl get请求中提取“ SALECODE”和“ PRICE”以供以后的MySql比较并插入(如果不同),结果是什么类型的JSON?
?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"];
?>
样品响应:
{
"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"
}
]
}
]
}
答案 0 :(得分:1)
尝试改用$json['result'][0]['DATASET'][0]['SALECODE']
。
如果您仔细查看响应数据,您会发现它是高度嵌套的。 result
也是一个数组,因此您需要从中选择一项。在这种情况下,只有一个。如果还有其他内容,则需要遍历$json['result']
。
答案 1 :(得分: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'];
?>
答案 2 :(得分:0)