我正在实现上传。我的API在我的网站。它工作得很好,图像被上传,但我得到一个JSON响应,如..
{"status_code":200,"status_txt":"OK","data":{"img_name":"8zN9G.jpg","img_url":"http:'/'/s1.uploads.im'/8zN9G.jpg","img_view":"http:'/'/uploads.im'/8zN9G.jpg","img_width":"954","img_height":"1421","img_attr":"width='"954'" height='"1421'"","img_size":"327.8 KB","img_bytes":335711,"thumb_url":"http:'/'/s1.uploads.im'/t'/8zN9G.jpg","thumb_width":360,"thumb_height":536,"source":"base64 image string","resized":"0","delete_key":"8c9bd7ab84a7bd6f"}}
,不知道如何将这些数据解析到数据库。我想在我的数据库中插入上传图像的url。
可以使用json_decode
$response = '{"status_code":200,"status_txt":"OK","data":{"img_name":"8zN9G.jpg","img_url":"http:'/'/s1.uploads.im'/8zN9G.jpg","img_view":"http:'/'/uploads.im'/8zN9G.jpg","img_width":"954","img_height":"1421","img_attr":"width='"954'" height='"1421'"","img_size":"327.8 KB","img_bytes":335711,"thumb_url":"http:'/'/s1.uploads.im'/t'/8zN9G.jpg","thumb_width":360,"thumb_height":536,"source":"base64 image string","resized":"0","delete_key":"8c9bd7ab84a7bd6f"}}';
$arr = json_decode($response, true);
echo "<pre>";
print_r($arr);
echo $arr['data']['img_url'];
输出:
Array
(
[status_code] => 200
[status_txt] => OK
[data] => Array
(
[img_name] => 8zN9G.jpg
[img_url] => http://s1.uploads.im/8zN9G.jpg
[img_view] => http://uploads.im/8zN9G.jpg
[img_width] => 954
[img_height] => 1421
[img_attr] => width="954" height="1421"
[img_size] => 327.8 KB
[img_bytes] => 335711
[thumb_url] => http://s1.uploads.im/t/8zN9G.jpg
[thumb_width] => 360
[thumb_height] => 536
[source] => base64 image string
[resized] => 0
[delete_key] => 8c9bd7ab84a7bd6f
)
)
http://s1.uploads.im/8zN9G.jpg