简单的 Curl 请求不起作用
我有一个函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您已将 RETURNTRANSFER 设置为 TRUE,这意味着curl 返回其输出而不是直接输出它。但是,您没有在变量中捕获该输出,因此它掉在地板上。
您有两个选择:
a) 删除 ob_*() 函数以删除 PHP 缓冲,然后执行
此操作,
$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
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.
尝试添加一行:
FALSE 来阻止 cURL 验证对等方的证书。可以使用 CURLOPT_CAINFO 选项指定要验证的备用证书,也可以使用 CURLOPT_CAPATH 选项指定证书目录。
Try by adding a row:
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.
发送没有标头的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