【问题标题】:cURL Passing Parameters - PHP - LaravelcURL 传递参数 - PHP - Laravel
【发布时间】:2018-07-08 20:22:51
【问题描述】:

我正在使用这个 cURL 函数来发送一组必须由 api 接收的数据。但是 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;

}

我尝试在发送前输出数据,它显示了正确的数据。另一个卷曲部分有效。知道为什么不通过 curl 发送数据吗?

【问题讨论】:

  • API 期望 post 还是 get?
  • 它期望得到
  • 使用邮递员测试你的url,检查是否正确?
  • 那你为什么用CURLOPT_POSTFIELDS
  • 是的..那是错误。我正在使用 POST 属性。谢谢

标签: php json laravel http curl


【解决方案1】:

由于您不需要使用 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;

}

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 2015-01-04
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多