简体   繁体   English

cURL传递参数-PHP-Laravel

[英]cURL Passing Parameters - PHP - Laravel

I am using this cURL function to send a set of data that has to be received by the api. 我正在使用此cURL函数来发送一组必须由api接收的数据。 However the api receives null. 但是,api接收到null。

public static function httpPost($url,$type,$params)
{
    $postData = '';

    foreach ($params as $k => $v) {
        $postData .= $k . '=' . $v . '&';
    }
    $postData = rtrim($postData, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    $output = curl_exec($ch);

    curl_close($ch);
    return $output;

}

I have tried outputting the data before sending and it shows correct data. 我尝试在发送之前输出数据,它显示正确的数据。 The other curl part works. 另一个卷曲部分起作用。 Any idea why data is not being sent through curl? 知道为什么不通过curl发送数据吗?

Since you do not need to use POST, unset that curl option and use PHP http_builder to build the URL 由于不需要使用POST,因此请取消设置curl选项,然后使用PHP http_builder来构建URL。

public static function httpPost($url,$type,$params)
{
    // Clean URL + append "?" at the end of URL + append the query
    $url = rtrim($url,"?") . "?" . http_build_query($params);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    $output = curl_exec($ch);

    curl_close($ch);
    return $output;

}

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

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