Perfex CRM API:PUT方法更新客户信息失败问题求助
Perfex CRM API更新客户报错排查与解决
我尝试通过Perfex CRM的API更新客户信息,已反复确认authtoken和客户ID无误,但用PHP CURL发起PUT请求后,收到错误响应:
{"status": false,"message": "Data Not Acceptable OR Not Provided"}
注:已确认必须用authtoken请求头而非文档里提到的Authorization,用后者会返回“Token is not defined.”。
问题代码(PUT更新客户)
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://example.com/api/customers/39', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => array('company' => 'test'), CURLOPT_HTTPHEADER => array( 'authtoken: example' ), )); curl_exec($curl); curl_close($curl);
正常工作的参考代码(POST创建联系人)
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://example.com/api/contacts/', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array( 'customer_id' => $userid, 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'phonenumber' => $phonenumber, 'password' => $password, 'is_primary' => 'on', 'donotsendwelcomeemail' => 'on', 'permissions' => array("1", "2", "3", "4", "5", "6") ), CURLOPT_HTTPHEADER => array( 'Authtoken: example' ), )); curl_exec($curl); curl_close($curl);
问题原因与解决方法
问题出在PUT请求的参数传递格式上:Perfex CRM的PUT更新接口要求以JSON格式传递数据,而当前代码用数组传递,CURL会自动编码为multipart/form-data表单格式,服务器无法识别这种格式,因此返回数据不接受的错误。
修正后的代码
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://example.com/api/customers/39', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'PUT', // 将数据转为JSON字符串 CURLOPT_POSTFIELDS => json_encode(array('company' => 'test')), CURLOPT_HTTPHEADER => array( 'authtoken: example', // 明确告诉服务器请求体是JSON格式 'Content-Type: application/json' ), )); $response = curl_exec($curl); // 可打印响应排查问题 echo $response; curl_close($curl);
补充说明
- 表单数组格式(
multipart/form-data)常用于POST创建类接口,但PUT更新接口大多要求JSON格式,这是很多CRM API的常见差异。 - 必须添加
Content-Type: application/json请求头,确保服务器正确解析请求体内容。
内容的提问来源于stack exchange,提问作者ILG
相关产品推荐
相关产品推荐

