调用未定义的函数curl_errorno(),但curl已安装并正常工作


Call to undefined function curl_errorno(), but cURL is installed and works

我正在尝试获取cURL错误号,但curl_errorno()函数似乎不起作用。如果我做一个单行脚本:

curl_errorno();

我得到这个错误:

调用未定义的函数curl_errorno()。。。

  • cURL已安装。。。我可以用它来提出请求
  • PHP 5.3.6(如PHP.ini所述)
  • cURL 7.19.7(如php.ini所述)
  • 我的配置命令包含--with-curl

关于为什么curl_errorno()不可用,有什么想法吗?

curl_errno(); 

不是

curl_errorno(); 

http://www.jonasjohn.de/snippets/php/curl-example.htm

function curl_download($Url){
    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }
    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();
    // Now set some options (most are optional)
    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);
    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    // Download the given URL, and return output
    $output = curl_exec($ch);
    // Close the cURL resource, and free system resources
    curl_close($ch);
    return $output;
}

使用它…

print curl_download('http://www.example.org/');

也许试试看,如果它有效,也许是你以前的代码有问题?

如果您对CURL有任何问题,或者CURL扩展没有安装在您的服务器上,那么只需使用以下代码

function get_web_page( $url )
{
    $options = array( 'http' => array(
        'user_agent'    => 'spider',    // who am i
        'max_redirects' => 10,          // stop after 10 redirects
        'timeout'       => 120,         // timeout on response
    ) );
    $context = stream_context_create( $options );
    $page    = @file_get_contents( $url, false, $context);
    $result  = array( );
    if ( $page != false )
        $result['content'] = $page;
    else if ( !isset( $http_response_header ) )
        return null;    // Bad url, timeout
    // Save the header
    $result['header'] = $http_response_header;
    // Get the *last* HTTP status code
    $nLines = count( $http_response_header );
    for ( $i = $nLines-1; $i >= 0; $i-- )
    {
        $line = $http_response_header[$i];
        if ( strncasecmp( "HTTP", $line, 4 ) == 0 )
        {
            $response = explode( ' ', $line );
            $result['http_code'] = $response[1];
            break;
        }
    }
    return $result;
}