获取具有curl,密码和用户名的外部网页

时间:2011-04-06 04:32:05

标签: php curl passwords username

使用curl和php

将网页检索为字符串

代码:

$ch = curl_init();
    // the external url

    $url = 'http://cursos.puc.cl/eaa220a-1/correo/main.html';
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');  
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: en-us,en;q=0.5", "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
    ));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    // get the source code
    $html = curl_exec($ch);

然而,该网页要求输入用户名和密码。我不知道如何管理这个。 THX!

1 个答案:

答案 0 :(得分:2)

来自here

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

您可以详细了解curl_setopt() here