PHP cURL请求SIP呼叫转移接口报错无法解析内容如何解决
错误原因
有两个核心问题导致接口无法解析请求:
- 请求数据格式错误:你声明了请求体格式是
application/json,但实际使用http_build_query()将数组转为了application/x-www-form-urlencoded格式的表单字符串,格式不匹配导致服务端无法解析JSON内容。 - 请求数据结构错误:原cURL命令中
forwardings字段是对象数组[{"destination":...}],但你的PHP代码中forwardings直接是单个关联数组,转JSON后结构和接口要求不符。
修正后的代码
$ch = curl_init(); $churl = 'https://api.sipgate.com/v2/w0/phonelines/p0/forwardings'; // 修正数据结构:forwardings对应二维索引数组,转JSON后就是要求的数组格式 $chdata = array( 'forwardings' => array( array( 'destination' => '+447979123456', 'timeout' => 0, 'active' => true ) ) ); // 直接转JSON字符串,不要用http_build_query $chdata2 = json_encode($chdata); curl_setopt($ch, CURLOPT_URL, $churl); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-type: application/json", "Accept: application/json", "Authorization: Basic abcdefABCDEFmysecretkey123456" )); curl_setopt($ch, CURLOPT_POSTFIELDS, $chdata2); $json = curl_exec($ch); echo $json; curl_close($ch);
内容的提问来源于stack exchange,提问作者Michael Virks
相关产品推荐
相关产品推荐

