繁体   English   中英

设置空的CURLOPT_POSTFIELDS

[英]Setting empty CURLOPT_POSTFIELDS

我正在尝试使用公共库函数来使用curl发送GET和POST请求。

function myCurl($url, $postData = null) 
{
    // Sets some default curl options here
    $options = array(
        ...
        CURLOPT_POSTFIELDS => $postData,
        ...
    );

    $ch = curl_init();
    // this works and sends GET
    // unset($options[CURLOPT_POSTFIELDS]); 

    curl_setopt_array($ch, $options);

    curl_exec($ch);
    curl_close($ch);
}

如果设置了$postData ,我希望此函数发送POST,否则将其视为GET 但是,即使我将CURL_POSTFIELDS设置为null ,这也会发送POST。 取消设置CURL_POSTFIELDS选项数组元素将起作用并发送GET 但是,有没有办法保持CURL_POSTFIELDS curl选项并将其设置为默认的空值并使其发送GET请求?

$options = array(
    ...
);
if(!empty($postData)){
    $options[CURLOPT_POSTFIELDS]=$postData;
}

或者,以下方法应该可行,但这是愚蠢的imo:

$options = array(
    ...
    CURLOPT_POSTFIELDS => $postData,
    CURLOPT_HTTPGET => empty($postData)
    ...
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM