Answer the question
In order to leave comments, you need to log in
How to process json response in PHP?
Hello!
I am writing a script for processing requests related to the cost of spare parts for cars. There is a provider web service that gives a response in JSON format, when I try to use the standard php function json_decode , I get null in the output. Where to dig, help? I am new to PHP
@header('Content-type:application/json;charset=windows-1251');
$dt="MB831042"; //номер детали, в будущем будет заменено на $_POST['detailnumb'];
$link="http://www.japarts.ru/?id=ws;action=search2;login=login=test;pass=testws;detailnum=".$dt.";cross=1;crosslimit=10;rowlimit=2;";
$out=file_get_contents($link);
echo $out;
[{"priceid":"25957093572","crossid":"7134851","makename":"Mitsubishi","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"4339.89","quantity":"314","lot":"1","time":"17","timegar":"67","country":"Америка","supcode":"1639","statistic":"64","remark":""},{"priceid":"9702723155","crossid":"7134851","makename":"555","detailnum":"SI7720","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"2351.87","quantity":"50","lot":"1","time":"0","timegar":"1","country":"Москва","supcode":"9189","statistic":"94","remark":""},{"priceid":"27164027551","crossid":"9616218","makename":"CTR","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"1879.49","quantity":"3","lot":"1","time":"1","timegar":"2","country":"Москва","supcode":"9588","statistic":"82","remark":""},{"priceid":"25957093572","crossid":"9616218","makename":"Mitsubishi","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"4339.89","quantity":"314","lot":"1","time":"17","timegar":"67","country":"Америка","supcode":"1639","statistic":"64","remark":""},{"priceid":"24291495020","crossid":"38256320","makename":"GMB","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"1325.20","quantity":"7","lot":"1","time":"1","timegar":"3","country":"Москва","supcode":"8617","statistic":"100","remark":""},{"priceid":"24975731427","crossid":"38256320","makename":"GMB","detailnum":"06040540","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"931.79","quantity":"10","lot":"1","time":"0","timegar":"2","country":"Москва","supcode":"0147","statistic":"98","remark":""},{"priceid":"23348803024","crossid":"19452469","makename":"SH Auto Parts","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"3389.51","quantity":"1","lot":"1","time":"1","timegar":"3","country":"Москва","supcode":"7861","statistic":"100","remark":""},{"priceid":"9789743274","crossid":"21593619","makename":"555","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"2660.44","quantity":"3","lot":"1","time":"6","timegar":"6","country":"Москва","supcode":"9251","statistic":"71","remark":""},{"priceid":"25957093572","crossid":"21593619","makename":"Mitsubishi","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"4339.89","quantity":"314","lot":"1","time":"17","timegar":"67","country":"Америка","supcode":"1639","statistic":"64","remark":""},{"priceid":"23533056835","crossid":"28592791","makename":"LONGHO","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"2717.31","quantity":"1","lot":"1","time":"15","timegar":"4","country":"Москва","supcode":"9956","statistic":"33","remark":""},{"priceid":"23738053148","crossid":"56424522","makename":"ZHONGXING","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"1104.64","quantity":"3","lot":"1","time":"6","timegar":"6","country":"Москва","supcode":"10862","statistic":"88","remark":""},{"priceid":"23738053150","crossid":"39850929","makename":"Kapars","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"4329.98","quantity":"2","lot":"1","time":"3","timegar":"6","country":"Россия","supcode":"0676","statistic":"95","remark":""},{"priceid":"23554710768","crossid":"51969821","makename":"Central","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"1890.84","quantity":"8","lot":"1","time":"3","timegar":"3","country":"Москва","supcode":"10179","statistic":"100","remark":""},{"priceid":"23230030666","crossid":"46203277","makename":"SRR","detailnum":"MB831042","detailname":"РЫЧАГ МАЯТНИКОВЫЙ","pricerur":"1588.51","quantity":"5","lot":"1","time":"15","timegar":"17","country":"Москва","supcode":"6081","statistic":"98","remark":""}]
$result=json_decode($out,true);
var_dump($result);
Answer the question
In order to leave comments, you need to log in
I didn't know about this feature. Thank you!
It turned out that the data for json_decode should be submitted only in utf-8 encoding, but it was win-1251.
In the end, after
$out=file_get_contents($link);
$out = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($out));
$r=json_decode($out,true);
var_dump($r);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question