You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP cURL请求SIP呼叫转移接口报错无法解析内容如何解决

错误原因

有两个核心问题导致接口无法解析请求:

  1. 请求数据格式错误:你声明了请求体格式是application/json,但实际使用http_build_query()将数组转为了application/x-www-form-urlencoded格式的表单字符串,格式不匹配导致服务端无法解析JSON内容。
  2. 请求数据结构错误:原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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 11:24:00