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

cUrl报CURLE_URL_MALFORMAT错误但Postman运行正常的解决方案

PHP cURL CURLE_URL_MALFORMAT错误修复方案

问题现象

Postman中可正常执行的请求,在PHP代码中调用cURL函数执行时报错,返回错误码3:CURLE_URL_MALFORMAT,使用的cURL版本为7.79.1,单独调整CURLOPT_RETURNTRANSFER、CURLOPT_POST等参数无法解决问题。

原有错误代码

private function cUrl_CreateContactFastFood($merchant_token, $arr)
{
    $status = "active";                         
    $Contact_name = $arr["customerName"];               
    $Contact_email = '';
                    
    if($Contact_email=='') {
            $Contact_email= 'FastFoodCustomer@ttt.com'; 
    }
            
    $Contact_phone = $arr["phoneNumber"];   
    $Contact_address = $arr["endAddressResolved"];                  

    $url = 'http://dostavka-bg.com/api_services/insert_contact';
    $curl_params = '?keys=f74192da825962d3b1c2b2aa616ab68b&merchant_token='.$merchant_token.'&name='.$Contact_name.'&email='.$Contact_email.'&phone='.$Contact_phone.'&address='.$Contact_address.'&status='.$status;
                       
    $ch = curl_init();
    $headers = array(
      'Accept: */*',
      'User-Agent: ',
      'Accept-Encoding: gzip, deflate, br',
      'Host: dostavka-bg.com',
      'Connection: keep-alive'
    );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url.$curl_params);

    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }

    curl_close($ch);
}

修复后可正常运行的代码

private function cUrl_CreateContactFastFood($merchant_token, $arr)
{
    $status = "active";
                
    $Contact_name = $arr["customerName"];

                    
    $Contact_email = '';//$arr["customer"]["invoicing_details"]["company_address"];
    if($Contact_email==''){
         $Contact_email= 'FastFoodCustomer@ttt.com';                                
    }
            
    $Contact_phone = $arr["phoneNumber"];   
    $Contact_address = $arr["endAddressResolved"];                  

    $url = 'http://dostavka-bg.com/api_services/insert_contact';
    
    $curl_params = '?keys=f74192da825962d3b1c2b2aa616ab68b&merchant_token='.urlencode($merchant_token).'&name='.urlencode($Contact_name).
    '&email='.urlencode($Contact_email).'&phone='.urlencode($Contact_phone).'&address='.urlencode($Contact_address).'&status='.urlencode($status);
    
    $ch = curl_init();
    
    $headers = array(
      'Accept: */*',
      'User-Agent: ',
      'Accept-Encoding: gzip, deflate, br',
      'Host: dostavka-bg.com',
      'Connection: keep-alive'
    );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url.$curl_params);
    curl_setopt($ch, CURLOPT_POST, 1);
    
    $data = curl_exec($ch); 

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    
    $info = curl_version();
 
    curl_close($ch);
    return true;
}

修复说明

  • 核心错误原因:URL拼接的动态参数没有做URL编码,包含特殊字符时会导致最终请求URL格式不合法,触发CURLE_URL_MALFORMAT错误,所有动态参数使用urlencode编码后再拼接即可解决
  • 补充设置CURLOPT_POST参数为1,匹配接口要求的请求类型

内容的提问来源于stack exchange,提问作者Даяна Димитрова

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 19:57:01