简单的 Curl 请求不起作用

发布于 2024-11-02 18:06:18 字数 834 浏览 1 评论 0原文

我有一个函数 make_curl_request 来发出curl 请求。

/**
 * General Function to make curl request */
function make_curl_request($url, $data)
{
    ob_start();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_exec($ch);
    curl_close($ch);
    $strCurlResponse = ob_get_contents();
    ob_end_clean();
    return $strCurlResponse;
}

我这样称呼它:

$strGatewayResponse = make_curl_request( REQUEST_URL, compact('strMobileNo', 'strKeywords', 'strApiKey') );

我尝试了这些事情,但无法让我的代码正常工作。目前它只是返回 string("") 作为输出。我哪里出错了?

我的目标是简单地将一些数据发布到位于其他域的下一页并获取其 xml 响应并解析并显示它。还有其他简单又好的解决办法吗?

I have a function make_curl_request to make curl request.

/**
 * General Function to make curl request */
function make_curl_request($url, $data)
{
    ob_start();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_exec($ch);
    curl_close($ch);
    $strCurlResponse = ob_get_contents();
    ob_end_clean();
    return $strCurlResponse;
}

I am calling it like:

$strGatewayResponse = make_curl_request( REQUEST_URL, compact('strMobileNo', 'strKeywords', 'strApiKey') );

I tried the things but can't get my code working fine. Currently its just return string("") as the output. Where am i going wrong?

My target is to simple post few data to next page located on other domain and get its xml response and parse it and display it. Is there any other simple and good solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜夜流光相皎洁 2024-11-09 18:06:18

问题是您已将 RETURNTRANSFER 设置为 TRUE,这意味着curl 返回其输出而不是直接输出它。但是,您没有在变量中捕获该输出,因此它掉在地板上。

您有两个选择:

a) 删除 ob_*() 函数以删除 PHP 缓冲,然后执行

$data = curl_exec($ch)
if ($data === FALSE) {
    die("Curl failed: " . curL_error($ch));
}

此操作,$data 包含您获取的 URL 的内容。

b) 删除 RETURNTRANSFER 选项,并让curl 执行正常的“直接输出到客户端”操作,然后由 PHP 输出缓冲捕获。

The problem is that you've got RETURNTRANSFER set to TRUE, which means curl returns its output instead of directly outputting it. However, you're not capturing that output in a variable, so it's dropping on the floor.

You've got two options

a) remove the ob_*() functions to remove the PHP buffering and then do

$data = curl_exec($ch)
if ($data === FALSE) {
    die("Curl failed: " . curL_error($ch));
}

after which $data contains the contents of the URL you've fetched.

b) remove the RETURNTRANSFER option, and let curl do its normal "output to client directly" thing, which then gets captured by the PHP output buffering.

拥抱我好吗 2024-11-09 18:06:18

尝试添加一行:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

FALSE 来阻止 cURL 验证对等方的证书。可以使用 CURLOPT_CAINFO 选项指定要验证的备用证书,也可以使用 CURLOPT_CAPATH 选项指定证书目录。

Try by adding a row:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.

叹沉浮 2024-11-09 18:06:18

发送没有标头的curl POST 是不行的。
请找到以下链接。这可能有帮助
OAuth、PHP、Rest API 和curl 给出 400 错误请求

it's not ok to send a curl POST without a header.
please find the following link. it may help
OAuth, PHP, Rest API and curl gives 400 Bad Request

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文