PHP cURL发送空参数 [英] PHP cURL sending empty parameters

查看:73
本文介绍了PHP cURL发送空参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cURL对我来说是新的.我正在尝试通过PHP cURL集成一个api.我尝试访问的api要求将参数作为键值对(而不是json)发送.他们在文档中的示例cURL请求为:

cURL is new to me. I'm trying to integrate an api via PHP cURL. The api that I'm trying to access requires that parameters be sent as key-value pairs, not json. Their example cURL request in their docs is:

curl -i -X POST -d  'api_key=my_api_key' -d 
'email=john@doe.com' -d "first_name=Joe" -d "last_name=Doe" -d 
"cust_id=cus_401" 
https://serviceurl.com/api/create

我的代码显然正在向其api发送空参数.

My code is apparently sending empty parameters to their api.

    $service_url = 'https://serviceurl.com/api/create';

    $curl = curl_init($service_url);

    $email = $this->session->userdata('email');

    $postArray = array(
        'api_key' => 'my_api_key',
        'email' => $email,
    );

    $curl_post_data = $postArray;
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);


    $curl_response = curl_exec($curl);
    if ($curl_response === false) {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }
    curl_close($curl);
    echo $curl_response;
    echo $info;

任何建议将不胜感激.

推荐答案

您的curl php代码以 multipart/form-data 格式发送数据,但是从cli调用示例,api需要 application/x-www-form-urlencoded 格式的数据.

your curl php code is sending the data in multipart/form-data format, but as evident by their cli invocation example, their api wants the data in application/x-www-form-urlencoded format.

如curl_setopt文档所述,当您给CURLOPT_POSTFIELDS时一个数组,它将自动编码为 multipart/form-data ,如果您给它一个字符串,则会自动采用 application/x-www-form-urlencoded ,以及他们的curl cli调用正在使用的内容.

as explained by the curl_setopt docs, when you give CURLOPT_POSTFIELDS an array, it will automatically be encoded to multipart/form-data, if you give it a string, application/x-www-form-urlencoded will automatically be assumed, and is what their curl cli invocation is using.

幸运的是,PHP具有专用于将数组编码为 application/x-www-form-urlencoded 格式的专用功能,称为 http_build_query ,因此 curl_setopt($ curl,CURLOPT_POSTFIELDS,http_build_query($ curl_post_data)); 将解决您的明显发送空参数的问题.

lucky for you, PHP has a dedicated function for encoding arrays to application/x-www-form-urlencoded format, called http_build_query, thus curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curl_post_data)); will fix your issue of apparently sending empty parameters.

此外,如果在设置任何选项时遇到问题,curl_setopt将返回bool(false),您的代码将完全忽略该布尔值,并且不会被注意到,您应解决此问题,考虑使用一个错误捕获的setopt包装器,喜欢

also, if there is a problem setting any of your options, curl_setopt will return bool(false), which your code is completely ignoring, and would go unnoticed, you should fix that, consider using an error-catching setopt wrapper, like

function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
    $ret=curl_setopt($ch,$option,$value);
    if($ret!==true){
        //option should be obvious by stack trace
        throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' . $ch .'. curl_error: '.curl_error($ch) );
    }
    return true;
}

这篇关于PHP cURL发送空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆