2013-09-26 28 views
0

發送我有以下叫login.php簡單的網頁形式,它包含:PHP捲曲 - 頁面的網頁報廢需要登錄 - 通過POST

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<meta name="robots" content="noindex,nofollow"> 
    <meta http-equiv="x-dns-prefetch-control" content="off"> 
</head> 
    <form action="action.php" method="post"> 
    <!-- Input: Input box --> 
    Name:  <input name="userName" type="text"/> 
    <br> 
    Password: <input name="userPassword" type="password"/> 
    <br> 
    <!-- Submit form --> 
    <input type="submit"/> <input type="reset"/> 
    </form> 
</body> 
</html> 

然後,我有很簡單的文件action.php它處理傳遞給它通過數據POST,這裏是代碼:

<?php 
print_r ($_POST); 
?> 

這完美的作品,如果我嘗試登錄爲用戶"foo"和密碼"bar"我得到:

Array ([userName] => foo [userPassword] => bar) 

我想要的是能夠通過curl直接發送POST內容到action.php。所以,我有第三個文件名爲scrapper.php它的代碼是在這裏:

<?php 

// SLIGHTLY MODIFIED CODE FROM: http://www.phpcodester.com/2011/01/scraping-a-password-protected-website-with-curl/ 
$ch=login('http://localhost/scrapper_post/action.php','userName=foo&userPassword=bar'); 
$html=downloadUrl('http://localhost/scrapper_post/action.php', $ch); 
echo $html; 

function downloadUrl($Url, $ch){ 
    curl_setopt($ch, CURLOPT_URL, $Url); 
    curl_setopt($ch, CURLOPT_POST, 0); 
    curl_setopt($ch, CURLOPT_REFERER, "http://localhost/scrapper_post/login.php"); 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    $output = curl_exec($ch); 
    return $output; 
} 

// ALSO TRIED WITH $postData ON SEPARATE LINES AS IT IS IN ORIGINAL TUTORIAL 
function login($url,$postData){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_POST, 1); 
    // ALSO TRIED WITH FOLLOWING, AS SUGGESTED IN ORIGINAL TUTORIAL COMMENTS: curl_setopt ($ch, CURLOPT_POSTFIELDS, urlencode($postData)); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData); 
    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    $store = curl_exec ($ch); 
    return $ch; 
} 
?> 

的問題是,當我叫scrapper.php我得到空$_POST變量action.php文件。換句話說,scrapper.php不會發送任何POST數據到action.php,我不知道爲什麼。這整個剛剛開始爲需要登錄的頁面編寫較大的網頁刮板,但正如您所看到的,我一開始就堅持不懈。謝謝。

回答

0

您不需要downloadUrl()功能,您的login()功能已經登錄並獲取內容。不要在login()return $store;,這將是從網站的HTML

我對代碼的建議:

<?php 

$html=login('http://localhost/scrapper_post/action.php','userName=foo&userPassword=bar'); 
echo $html; 

function login($url,$postData){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($postData)); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_REFERER, "http://localhost/scrapper_post/login.php"); 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    $output = curl_exec($ch); 
    curl_close($ch); 
    return $output; 
} 
?>