在API集成中......我需要将json数据传递给CURL。 以下是API规范的外观
curl -X POST \
-H "Authorization: Bearer {token}" \
-H "Content-Type: application/json" -d '
{
"from": "12345",
"to": [
"123456789",
],
"body": "Hi there! How are you?"
}' \
"https://api.service.com/xms/v1/batches"
这是我的数组
private $post_data = array('from' => '91875222222',
'to' => array('918757022222'),
'body' => 'I am test robot.');
内部函数callAPI()...将其转换为json格式以将数据传递给url
$test = json_encode(array($this->post_data), JSON_FORCE_OBJECT);
curl_setopt($curl, CURLOPT_POSTFIELDS, $test);
我正在尝试这种方式......但是......当我尝试打印$ test数据时,它会输出
{\“from \”:\“918757095588 \”,\“to \”:[\“918757095588 \”],\“body \”:\“我是 智能测试机器人。\“}”}
没有成功。不确定缺少什么。我已将标头授权设置为:
$headers = array(
'Content-Type:application/json',
'Authorization: Bearer 2cfe3a1d8899019db3512222a9fecb'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);