php curl_exec() 总是返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8227909/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:12:51  来源:igfitidea点击:

curl_exec() always returns false

phpcurl

提问by Adithya

I have written this simple piece of code :

我写了这段简单的代码:

$ch = curl_init();

//Set options
curl_setopt($ch, CURLOPT_URL, "http://www.php.net");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$website_content = curl_exec ($ch);

In my case $website_contentcomes as false. Can anyone suggest/advice something what could be going wrong?

在我的情况下,$website_content作为false. 任何人都可以建议/建议可能出什么问题吗?

回答by Linus Kleen

Error checking and handling is the programmer's friend. Check the return values of the initializing and executing cURL functions. curl_error()and curl_errno()will contain further information in case of failure:

错误检查和处理是程序员的朋友。检查初始化和执行 cURL 函数的返回值。curl_error()并且curl_errno()将包含在发生故障时的详细信息:

try {
    $ch = curl_init();

    // Check if initialization had gone wrong*    
    if ($ch === false) {
        throw new Exception('failed to initialize');
    }

    curl_setopt($ch, CURLOPT_URL, 'http://example.com/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt(/* ... */);

    $content = curl_exec($ch);

    // Check the return value of curl_exec(), too
    if ($content === false) {
        throw new Exception(curl_error($ch), curl_errno($ch));
    }

    /* Process $content here */

    // Close curl handle
    curl_close($ch);
} catch(Exception $e) {

    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);

}


*The curl_init()manualstates:

*curl_init()手动状态:

Returns a cURL handle on success, FALSEon errors.

成功时返回 cURL 句柄,错误时返回FALSE

I've observed the function to return FALSEwhen you're using its $urlparameter and the domain could not be resolved. If the parameter is unused, the function mightnever return FALSE. Always check it anyways, though, since the manual doesn't clearly state what "errors" actually are.

我观察到该函数FALSE在您使用其$url参数并且无法解析域时返回。如果该参数未使用,则该函数可能永远不会返回FALSE。但是,无论如何都要检查它,因为手册没有明确说明“错误”实际上是什么。

回答by DanielaG

In my case I need to set VERIFYHOSTand VERIFYPEERto false, like this:

在我来说,我需要设置VERIFYHOSTVERIFYPEERfalse,是这样的:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

before the call to curl_exec($ch).

在调用之前curl_exec($ch)

Because i am working between two development environments with self-assigned certificates. With valid certificates there is no need to set VERIFYHOSTand VERIFYPEERto falsebecause the curl_exec($ch)method will work and return the response you expect.

因为我在两个具有自分配证书的开发环境之间工作。使用有效的证书,无需设置VERIFYHOSTVERIFYPEERfalse因为该curl_exec($ch)方法将起作用并返回您期望的响应。

回答by CIRCLE

This happened to me yesterday and in my case was because I was following a PDF manual to develop some module to communicate with an API and while copying the link directly from the manual, for some odd reason, the hyphenfrom the copied link was in a different encoding and hence the curl_exec()was always returning falsebecause it was unable to communicate with the server.

这昨天发生在我身上,在我的情况下是因为我正在按照 PDF 手册开发一些模块来与 API 通信,并且在直接从手册中复制链接时,出于某种奇怪的原因,hyphen复制的链接位于不同的位置编码,因此curl_exec()总是返回,false因为它无法与服务器通信。

It took me a couple hours to finally understand the diference in the characters bellow:

我花了几个小时才终于明白以下字符的差异:

https://www.e‐example.com/api
https://www.e-example.com/api

Every time I tried to access the link directly from a browser it converted to something likehttps://www.xn--eexample-0m3d.com/api.

每次我尝试直接从浏览器访问链接时,它都会转换为类似https://www.xn--eexample-0m3d.com/api.

It may seem to you that they are equal but if you check the encoding of the hyphenshereyou'll see that the first hyphenis a unicode characters U+2010and the other is a U+002D.

在您看来它们是相等的,但是如果您检查hyphens此处的编码,您会看到第一个hyphen是 unicode 字符U+2010,另一个是U+002D

Hope this helps someone.

希望这可以帮助某人。