我需要檢索頁面的html內容(源),例如:www.google.com頁面。然後我可以使用:file_get_contents
或curl_init
與PHP。一旦我有密碼,PHP將檢索密碼所需頁面的內容?
正是因爲某人的問題之前,在這裏:
How do I get the HTML code of a web page in PHP?
但更多的則是對我來說,有些頁面是所需訪問權限。
但我已授予訪問權限並知道密碼。
(比方說,它要求的密碼與表單和密碼爲「ABCD」。)
讓我怎麼閱讀編程與PHP頁面?我找到了解決方案curl-setopt
suggested by Bekzat Abdiraimov below。然後現在我發佈了詳細的代碼在這裏,我發現某處並修改:
<?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");
?>
http://stackoverflow.com/questions/1304974/set-authorization-header-使用-php-and-curl – 2012-07-30 10:52:44
是的,我發現'curl-setopt'是正確的解決方案。即使您沒有發佈代碼,我也將其設置爲答案。但它確定,我發佈它。謝謝!! ;) – 2012-07-30 12:28:53