Ank*_*agi 4 php json web-services http
我需要使用以JSON格式响应的HTTP Web服务.考虑到Web服务的URL已知,我怎样才能在php中实现这一点?
Luc*_*emy 10
这是你应该做的:
$data = file_get_contents(<url of that website>);
$data = json_decode($data, true); // Turns it into an array, change the last argument to false to make it an object
Run Code Online (Sandbox Code Playgroud)
这应该能够将JSON数据转换为数组.
现在,解释它的作用.
file_get_contents()基本上获取远程或本地文件的内容.这是通过HTTP门户,因此您不会通过将此功能用于远程内容来违反隐私策略.
然后,当您使用时json_decode(),它通常会将JSON文本更改为PHP中的对象,但由于我们true为第二个参数添加了它,因此它返回一个关联数组.
然后你可以对数组做任何事情.
玩得开心!