相关文章推荐
踏实的紫菜汤  ·  Marshal.FreeHGlobal(In ...·  1 年前    · 
帅呆的韭菜  ·  5、Java ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams //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_content comes as false . Can anyone suggest/advice something what could be going wrong?

@LinusKleen If you post the curl_error() as an answer I'll upvote it as it allowed me to see what my problem was. Giles Roberts Nov 9, 2012 at 15:03

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:

try {
    $ch = curl_init();
    // Check if initialization had gone wrong*    
    if ($ch === false) {
        throw new Exception('failed to initialize');
    // Better to explicitly set URL
    curl_setopt($ch, CURLOPT_URL, 'http://example.com/');
    // That needs to be set; content will spill to STDOUT otherwise
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // Set more options
    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));
    // Check HTTP return code, too; might be something else than 200
    $httpReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    /* Process $content here */
} catch(Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
} finally {
    // Close curl handle unless it failed to initialize
    if (is_resource($ch)) {
        curl_close($ch);

* The curl_init() manual states:

Returns a cURL handle on success, FALSE on errors.

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

+1: Simple and straight trouble-shooting for curl in PHP on curl_exec FALSE return. - Curl Verbose Mode in PHP example – hakre Nov 9, 2012 at 20:10 It's implicitly closed when the variable goes out of scope, but sure. I made it a complete example. Thanks, @RafikBari. – Linus Kleen Apr 28, 2018 at 10:30 And make sure to call curl_close after curl_error and curl_errno, otherwise you'll get a false here, too. Happened to me. True story. – Wu Wei Jan 31, 2020 at 10:07 +1 In my case the error was related to HTTP 2.0 - HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1). cURL stopped working suddenly, and I had no idea why - nothing was changed on my end, nor on the remote end. Using curl_error($ch) helped debug it. – FiddlingAway Sep 1, 2022 at 13:30

In my case I need to set VERIFYHOST and VERIFYPEER to false, like this:

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

before the call to curl_exec($ch).

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

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 hyphen from the copied link was in a different encoding and hence the curl_exec() was always returning false because it was unable to communicate with the server.

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.

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

Hope this helps someone.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.