@IvanSerr

Как обработать json ответ в PHP?

Здравствуйте!

Пишу скрипт по обработке запросов, связанных со стоимостью запчастей для автомобилей. Есть веб-сервис поставщика, который дает ответ в формате JSON, при попытке использования стандартной php функции json_decode , получаю в выводе null. Куда копать, помогите ? Я новичок в 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;


Если вывести тупо $out, то выводит данные в формате JSON, например:

[{"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);


То выводит NULL.

Как быть дальше, что я делаю не так?
  • Вопрос задан
  • 687 просмотров
Решения вопроса 1
Sanasol
@Sanasol Куратор тега PHP
нельзя просто так взять и загуглить ошибку
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@IvanSerr Автор вопроса
Я не знал об этой функции. Спасибо!

Оказалось, что данные для json_decode должны подавать только в utf-8 кодировке, а была win-1251.

В итоге после

$out=file_get_contents($link);
$out = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($out));

$r=json_decode($out,true);
var_dump($r);


Выводит структуру массива.

Спасибо!
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы