PHP检索内容所需的密码页面,一旦我有密码?

PHP检索内容所需的密码页面,一旦我有密码?,php,curl,permissions,passwords,file-get-contents,Php,Curl,Permissions,Passwords,File Get Contents,我需要检索页面的html内容(源),例如:www.google.com页面。然后我可以用PHP使用:file\u get\u contents或curl\u init 正如前面有人提出的问题: 但对我来说,更重要的是,有些页面需要访问 但我已授予访问权限并知道密码。 (假设它使用表单询问密码,密码为“abcd”。) 那么,如何使用PHP以编程方式阅读这些页面呢 更新(答案,为我): 我用curl setopt找到了解决方案。现在我在这里发布了我在某处找到并修改的详细代码: <?php f

我需要检索页面的html内容(源),例如:www.google.com页面。然后我可以用PHP使用:
file\u get\u contents
curl\u init

正如前面有人提出的问题:

但对我来说,更重要的是,有些页面需要访问
但我已授予访问权限并知道密码。

(假设它使用表单询问密码,密码为“abcd”。)

那么,如何使用PHP以编程方式阅读这些页面呢

更新(答案,为我):
我用
curl setopt
找到了解决方案。现在我在这里发布了我在某处找到并修改的详细代码:

<?php
function curl_grab_page($url, $ref_url, $data, $login, $proxy, $proxystatus){
    if($login == 'true') {
        $fp = fopen("cookie.txt", "w");
        fclose($fp);
    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    if ($proxystatus == 'true') {
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }

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

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $ref_url);

    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_exec($ch);

    curl_setopt($ch,CURLOPT_URL,$ref_url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

    ob_start();
    $data = curl_exec($ch);
    ob_end_clean();

    curl_close($ch);
    return $data;
}

/*
 * $auth_processing_url .. is the posted 'action' url in login form like <form method=post action='http://www.abc.com/login.asp'> So it should be like: "http://www.abc.com/login.asp"
 * $url_to_go_after_login .. is the url you want to go (to be redireced) after login
 * $login_post_values .. are the form input names what Login Form is asking. E.g on form: <input name="username" /><input name="password" />. So it should be: "username=4lvin&password=mypasswd"
 */
echo curl_grab_page($auth_processing_url, $url_to_go_after_login, $login_post_values, "true",  "null", "false");
?>

看看如何使用饼干罐

当您第一次进行身份验证时,存储您的身份验证的“Cookie”将丢失(假设您尚未使用Cookie Jar),因此您发出的下一个请求将不知道您已登录

因此,您需要使用Cookie Jar来存储身份验证Cookie


这取决于所需的身份验证类型。如果它是广泛使用的基本身份验证类型,那么它就是添加到请求中的一个普通头。您可以看到技术细节。要使用
file\u get\u contents
向请求添加头,请使用,并通过示例说明其用法。

使用curl curl_setopt(资源$ch,整数$option,混合$value)


是的,我发现
curl setopt
是正确的解决方案。即使你没有发布代码,我也会将此设置为答案。但这已经足够了,我正在发布它。谢谢!!;)
option = CURLOPT_HTTPAUTH
value = choose auth type (CURLAUTH_BASIC, ...)