php 尝试访问本地文件时,什么会导致 curl 返回 false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12359100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what would cause curl to return false when trying to access a local file?
提问by codescribblr
This site has been up for several months now and has been working fine. I have a PHP page that creates an invoice from data in the url (e.g. viewinvoice.php?id=250 builds an invoice based on record 250). This page is accessible via a web browser and works fine.
这个网站已经上线几个月了,一直运行良好。我有一个 PHP 页面,它根据 url 中的数据创建发票(例如 viewinvoice.php?id=250 根据记录 250 生成发票)。此页面可通过 Web 浏览器访问并且运行良好。
On a completely different page (i.e. test.php) I'm trying to access that file via cURL. However, when I make the call and var_dump the results, I get bool(false).
在一个完全不同的页面(即 test.php)上,我试图通过 cURL 访问该文件。但是,当我拨打电话和 var_dump 结果时,我得到 bool(false)。
Here's the function that makes the cURL call:
这是进行 cURL 调用的函数:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
HOME is a constant that denotes the full url (e.g. http://www.example.com/).
HOME 是一个常量,表示完整的 url(例如http://www.example.com/)。
$invoice_contents = file_get_contents_curl(HOME.'viewinvoice.php?id=242');
echo $invoice_contents;
var_dump( $invoice_contents );
I've tried changing the url to an external url (i.e. http://www.google.com/) and the page loads just fine. I get google's home page. But any page that's in the same domain won't load. Is there a reason that this would happen?
我已经尝试将 url 更改为外部 url(即http://www.google.com/)并且页面加载得很好。我得到了谷歌的主页。但是在同一个域中的任何页面都不会加载。有什么理由会发生这种情况吗?
I am not the server admin, but I have root access to the server. I have not changed any settings recently, but the server admin may have upgraded the version of apache or php?
我不是服务器管理员,但我有服务器的 root 访问权限。我最近没有更改任何设置,但服务器管理员可能升级了apache或php的版本?
In any case, is there a setting I can modify to make this work again?
在任何情况下,是否有我可以修改的设置以使其再次工作?
P.S. I just tried making this exact call from an external server (different domain) and it works just fine.
PS我只是尝试从外部服务器(不同的域)进行这个确切的调用,它工作得很好。
回答by Alain Tiemblo
After your curl execution, put something like this :
在执行 curl 之后,输入如下内容:
echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
You'll see what failed during your curl execution.
您将看到 curl 执行期间失败的内容。
More info : curl_getinfocurl_errnocurl_error
回答by Ahadul islam
After execution put this variable to test your errors
执行后放置这个变量来测试你的错误
echo "<pre>";
var_dump( curl_getinfo($ch) ) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
回答by Cyborg
I had a similar problem where PHP curl returned false. The accepted answer dosent solve the problem.
我有一个类似的问题,PHP curl 返回 false。接受的答案确实解决了问题。
My case: I have a website which makes PHP curl calls to my private server to get data. It worked fine for 2 years but suddenly curl started to return false.
我的情况:我有一个网站,它使 PHP curl 调用我的私人服务器以获取数据。它工作了 2 年,但突然 curl 开始返回 false。
I tried to open my private server page in browser and it was working fine.
I tried PHP
file_get_contents("http://my.server/page.php");and was also working fine.
我试图在浏览器中打开我的私人服务器页面,它工作正常。
我尝试过 PHP
file_get_contents("http://my.server/page.php");并且工作正常。
But curl was still returning false.
但是 curl 仍然返回 false。
For debugging PHP curl I found a much better solution here: Php - Debugging Curl
为了调试 PHP curl,我在这里找到了一个更好的解决方案: Php - Debugging Curl
The problem: My ISP had issues with network and my internet connection speed dropped from 100Mbps to 1.5Mbps at that time - The PHP curl did not spend enough time to wait for the response.
问题:我的 ISP 有网络问题,当时我的互联网连接速度从 100Mbps 下降到 1.5Mbps - PHP curl 没有花足够的时间来等待响应。
The solution: I added this 2 lines in curl calls:
解决方案:我在 curl 调用中添加了这 2 行:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); //timeout in seconds
Which I found here: Setting Curl's Timeout in PHP
我在这里找到了:Setting Curl's Timeout in PHP

