【发布时间】:2018-09-22 02:53:50
【问题描述】:
我有以下问题。此结构中的数据
$transfer = {stdClass} [4]
module = "Member"
action = "create"
token = null
params = {stdClass} [3]
username = "Test"
email = "test@test.com"
password = "Test"
必须将 vi REST 发送到 REST 服务器。
因此,我使用 json_encode ($object) 对数据进行编码。 解码后的对象如下所示:
{"module":"Member","action":"create","token":null,"params":{"username":"Test","email":"test@test.com","password":"Test"}}
出于测试目的,我对编码结果进行解码以查看是否一切正常。这给了我正确的对象。
当我通过 curl 传输数据时,服务器会收到这个 json_encoded 数据:
{"module":"Member","action":"create","token":null,"params":{"username":"Test","email":"test@test.com" = ""
最后 json_decode($request) 出现以下错误:
json_decode() expects parameter 1 to be string, array given
curl 代码是:
$curl = curl_init($url);
// prepare curl for Basic Authentication
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$curl_post_data = json_encode($postdata);
$test = json_decode($curl_post_data); // for testing
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); <= $postdata = $transfer the object/array mentioned above
$curl_response = curl_exec($curl);
curl_close($curl);
怎么了?为什么 curl exec 到 REST 服务器后无法解码编码数据?
【问题讨论】:
-
请检查代码:- $object = '[{"module":"Member","action":"create","token":null,"params":{"username": "Test","email":"test@test.com","password":"Test"}}]'; $data = json_decode($object);字符串问题
-
检查码 Sunny 是什么意思?我现在还添加了 curl 语句。
标签: php arrays json rest decoding