我如何从PHP中的JSON服务器响应中获取数据

时间:2018-07-26 06:15:43

标签: php json

我正在用PHP发送JSON请求,并从服务器收到响应,如下所示:

{"success":true,"result":
{"items":
[{"woj":"łódzkie","powiat":"kutnowski","gmina":"Bedlno","kod":"99-        
311","miasto":"Adamów","id":"99-311Adamów167271172700"}]},
"error":null,
"unAuthorizedRequest":false}

我想从“ woj”和“ powiat”中选择一个内容,但是它不起作用。

我的代码如下:

$code = 'https://hetman.e4b.com.pl/api/services/app/kodPoczt/KodPocztInfo? 
 kod='.$kod;
 $ch = curl_init();
 //set the url, number of POST vars, POST data
 curl_setopt($ch,CURLOPT_URL, $code);
 curl_setopt($ch,CURLOPT_POST, true);
 curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 //execute post
 $result1 = curl_exec($ch);
 //close connection
 curl_close($ch);
 echo $result1;
 $wynik  = json_decode($result1, true);
 echo $wynik['result'][0]['woj'];   

2 个答案:

答案 0 :(得分:0)

显示您的完整代码...在示例中未初始化curl,未设置return_transfer。 $ result1的输出是什么?

无论如何

$wynik['result']['items'][0]['woj'];

答案 1 :(得分:0)

$ temp ='{                     “成功”:是的,                     “结果”:{                         “项目”:[{                             “ woj”:“łódzkie”,                             “ powiat”:“ kutnowski”,                             “ gmina”:“ Bedlno”,                             “ kod”:“ 99-311”,                             “ miasto”:“Adamów”,                             “ id”:“99-311Adamów167271172700”                         }]                     },                     “错误”:null,                     “ unAuthorizedRequest”:false                 }';

ng serve --project=myAppOne --port 4202