通过HTTP上的cURL(SSL),Cookie,用户名:密码检索网站内容时出现问题

时间:2011-02-20 07:06:47

标签: php cookies curl https

我遇到cURL问题,

问题是 - 无法通过HTTP(SSL)连接 - 无法存储cookie - 无法使用用户名登录:密码

'curl_setopt'上有层次结构吗?

我希望我能得到最好的答案。 提前致谢

<?php
function get_url($url,$username,$password)
{
$curl = curl_init();

$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";

$cookie = '/cookies.txt';
$timeout = 30;

curl_setopt($curl, CURLOPT_URL,             $url);
curl_setopt($curl, CURLOPT_USERAGENT,       "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($curl, CURLOPT_HTTPHEADER,      $header);
curl_setopt($curl, CURLOPT_ENCODING,        'gzip,deflate'); 
curl_setopt($curl, CURLOPT_AUTOREFERER,     true); 
curl_setopt($curl, CURLOPT_TIMEOUT,         10); 
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,  $timeout );
curl_setopt($curl, CURLOPT_COOKIEJAR,       $cookie);
curl_setopt($curl, CURLOPT_COOKIEFILE,      $cookie);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,  true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,  true );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,  false );    # required for https urls
curl_setopt($curl, CURLOPT_MAXREDIRS,       10 );
curl_setopt($curl, CURLOPT_HTTPAUTH,        CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD,         "$username:$password");

$responseHTML   = curl_exec($curl);
$response       = curl_getinfo( $curl );

curl_close($curl); // close the connection

//return $html; // and finally, return $html


if ($response['http_code'] == 301 || $response['http_code'] == 302)
{
    ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

    if ( $headers = get_headers($response['url']) )
    {
        foreach( $headers as $value )
        {
            if ( substr( strtolower($value), 0, 9 ) == "location:" )
                return get_url( trim( substr( $value, 9, strlen($value) ) ) );
        }
    }
}

if (
    (preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) 
    || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value))
    && $javascript_loop < 5
)
{
    return get_url( $value[1], $javascript_loop+1 );
}
else
{
    return $responseHTML; //array( $content, $response );
}
}


$url = 'https://blog.example.com/wp-login.php';
$user = 'admin';
$pass = 'thesecurepassword123';

// uses the function and displays the text off the website
$text = get_url($url,$user,$pass);
echo $text;
?>

1 个答案:

答案 0 :(得分:1)

您提供的用户名和密码用于HTTP身份验证(您是否曾访问过一个网站,其中弹出一个小方框,如警告框,说它需要用户名和密码?),但您的网址看起来像是在查询一个wordpress登录页面。这就是您无法使用该用户名和密码登录的原因。

我相信wordpress将登录参数作为POST变量 - 用户名为'log',密码为'pwd'。尝试将用户名和密码作为这些POST变量发送,这应该可行。