2011-11-22 13:51
浏览 282


I have written this simple piece of code :

$ch = curl_init();

//Set options
curl_setopt($ch, CURLOPT_URL, "");
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?

图片转代码服务由CSDN问答提供 功能建议


  $  ch = curl_init(); 
curl_setopt($ ch,CURLOPT_URL,“”); 
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); 
 $ website_content  = curl_exec($ ch); 

在我的情况下, $ website_content 来自 false 。 任何人 建议/建议可能出错的事情?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanpao4172 2012-11-09 15:44

    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');
        curl_setopt($ch, CURLOPT_URL, '');
        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
    } catch(Exception $e) {
            'Curl failed with error #%d: %s',
            $e->getCode(), $e->getMessage()),

    * 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.

    打赏 评论
  • donglei1616 2017-11-12 00:29

    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:


    Every time I tried to access the link directly from a browser it converted to something like

    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.

    打赏 评论
  • duancaozen6066 2019-06-04 12:23

    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.

    打赏 评论

相关推荐 更多相似问题