Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php拆分Json编码的数组_Php_Arrays_Json_Api - Fatal编程技术网

使用php拆分Json编码的数组

使用php拆分Json编码的数组,php,arrays,json,api,Php,Arrays,Json,Api,事实上,我是Api界的新手,我正在开发一个Api,它使用Curl请求获取并发回数据。获取时,将传递以下信息 {"posts":{"userinfo":{"fullname":"Precious Tom","user_name":"Kendrick","email":"tomprezine@gmail.com","gender":"Male","country":"Nigeria","city":"Port Harcourt","state":"Rivers","year":"1997","mon

事实上,我是Api界的新手,我正在开发一个Api,它使用Curl请求获取并发回数据。获取时,将传递以下信息

{"posts":{"userinfo":{"fullname":"Precious Tom","user_name":"Kendrick","email":"tomprezine@gmail.com","gender":"Male","country":"Nigeria","city":"Port Harcourt","state":"Rivers","year":"1997","month":"9","day":"6"}}}
内容类型:text/html;字符集=UTF-8

http代码:200

拆分阵列似乎是我的问题,如果以前有人问过这个问题,我很抱歉,但是请,我需要你的帮助。 谢谢

更多信息,这里是我的卷曲请求

<?php
session_start();
# data to be sent
$data = array(
'public_key' => 'pk_test_3gc9ffb0hccggf5f3b4e258da848343dff4ae900',
'app_name' => 'Circlepanda',
'app_id' => '2147483647'
);
$curl = curl_init();
# you can also set the url you wanna communicate with by setting
# $curl = curl_init('http://localhost/circlepanda');
  # We post Data
curl_setopt($curl, CURLOPT_POST, 1);
# Set the url path we want to call
 curl_setopt($curl, CURLOPT_URL, 'http://localhost:8888/circlepanda/api/userinfo');
# Make it so the data coming back is put into a string
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  # You can also bunch the above commands into an array if you choose using: curl_setopt_array
# Send the request
$result = curl_exec($curl);
# Get some cURL session information back
$info = curl_getinfo($curl);
echo '<br> content type: ' . $info['content_type'] . '<br>';echo 'http code: ' . $info['http_code'] . '<br>';
# Free up the resources $curl is using
curl_close($curl);
?>
我将传递一个变量,而不是直接数组。 尝试直接数组您的代码运行良好,在传递变量时出错,它停止工作

$city_names = json_decode('{"posts":{"userinfo":{"fullname":"Precious Tom","user_name":"Kendrick","email":"tomprezine@gmail.com","gender":"Male","country":"Nigeria","city":"Port Harcourt","state":"Rivers","year":"1997","month":"9","day":"6"}}}', true);

print_r($city_names);
ans

您有两种选择:

1-使用以下命令将json转换为对象:

2-使用以下命令将json转换为数组:


了解有关

的更多信息,那么问题是什么?显示迄今为止您尝试过的任何代码。{帖子:{用户信息:{全名:珍贵的汤姆,用户名:肯德里克,电子邮件:tomprezine@gmail.com,性别:男性,国家:尼日利亚,城市:哈科特港,州:里弗斯,年份:1997,月份:9,日期:6}',对;
Array
(
    [posts] => Array
        (
            [userinfo] => Array
                (
                    [fullname] => Precious Tom
                    [user_name] => Kendrick
                    [email] => tomprezine@gmail.com
                    [gender] => Male
                    [country] => Nigeria
                    [city] => Port Harcourt
                    [state] => Rivers
                    [year] => 1997
                    [month] => 9
                    [day] => 6
                )

        )

)

$city_names = json_decode($json, true);
print $arr['posts']['userinfo']['fullname'];
$obj = json_decode($json);
print $obj->posts->userinfo->fullname;
$arr = json_decode($json, true);
print $arr['posts']['userinfo']['fullname'];