2010-06-09 166 views
49

我剛剛使用cURL和它很難找到它的好資源。 我想要做的是登錄到遠程站點,通過捲曲做登錄表單,然後發回它是成功的。用PHP cURL登錄到遠程站點

我的代碼似乎並不工作,只試圖顯示該網站的主頁。

$username="[email protected]"; 
$password="mypassword"; 
$url="http://www.myremotesite.com/index.php?page=login"; 
$cookie="cookie.txt"; 

$postdata = "email=".$username."&password=".$password; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result; 
curl_close($ch); 

我在做什麼錯。這是工作後,我想重定向到另一個頁面,並從我的網站獲取內容。

+0

你確定cookie.txt可以通過你的腳本寫嗎? – Mark 2010-06-09 20:23:36

+0

你的'$ url'應該是gmail登錄名,而不是你自己的腳本。 – 2010-06-09 18:56:49

+0

這是登錄頁面。這是登錄表單所在的位置。我沒有試圖用這個腳本登錄到Gmail。 – 2010-06-09 19:12:08

回答

39

我已經讓它變好了一段時間,但稍後重新訪問它。由於這個問題被定期查看。這最終是我最終使用的爲我工作的。

//username and password of account 
$username = trim($values["email"]); 
$password = trim($values["password"]); 

//set the directory for the cookie using defined document root var 
$dir = DOC_ROOT."/ctemp"; 
//build a unique path with every request to store 
//the info per user with custom func. 
$path = build_unique_path($dir); 

//login form action url 
$url="https://www.site.com/login/action"; 
$postinfo = "email=".$username."&password=".$password; 

$cookie_file_path = $path."/cookie.txt"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_NOBODY, false); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 
//set the cookie the site has for certain features, this is optional 
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0"); 
curl_setopt($ch, CURLOPT_USERAGENT, 
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo); 
curl_exec($ch); 

//page with the content I want to grab 
curl_setopt($ch, CURLOPT_URL, "http://www.site.com/page/"); 
//do stuff with the info with DomDocument() etc 
$html = curl_exec($ch); 
curl_close($ch); 
+0

+1是的,這是正確的,如果您想在登錄後執行一些其他請求,例如獲取用戶的收件箱消息(登錄後),您應該像我的回答那樣做。請檢查它 – ncm 2014-01-16 19:12:35

+0

@imsiso是的,在我的實際代碼中,我導航了幾個URL以獲取其他數據。 – 2014-01-16 19:19:02

+0

我可以在twitter上使用它嗎? – sotirios 2015-12-14 22:58:53

14

查看登錄頁面的來源。尋找formHTML標記。在該標籤內的內容看起來像action=將該值用作$url,而不是表單本身的URL。

此外,當你在那裏,驗證輸入框被命名爲你有他們列出。

例如,一個基本的登錄表單將類似於:使用

<form method='post' action='postlogin.php'> 
    Email Address: <input type='text' name='email'> 
    Password: <input type='password' name='password'> 
</form> 

上述形式爲例,更改到$url值:

$url="http://www.myremotesite.com/postlogin.php"; 

確保你使用的值列在$postdata

$postdata = "email=".$username."&password=".$password; 

它應該只是fin即

16

我有同樣的問題,我發現這個答案on this website

而且我改變它只是一點點(在最後一行curl_close)

$username = 'myuser'; 
$password = 'mypass'; 
$loginUrl = 'http://www.example.com/login/'; 

//init curl 
$ch = curl_init(); 

//Set the URL to work with 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 

// ENABLE HTTP POST 
curl_setopt($ch, CURLOPT_POST, 1); 

//Set the post parameters 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password); 

//Handle cookies for the login 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 

//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL 
//not to print out the results of its query. 
//Instead, it will return the results as a string return value 
//from curl_exec() instead of the usual true/false. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

//execute the request (the login) 
$store = curl_exec($ch); 

//the login is now done and you can continue to get the 
//protected content. 

//set the URL to the protected file 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/protected/download.zip'); 

//execute the request 
$content = curl_exec($ch); 

curl_close($ch); 

//save the data to disk 
file_put_contents('~/download.zip', $content); 

我認爲這是你是對的看for.Am我?


和一個有用的相關問題。關於如何在cUrl中保持活動狀態:https://stackoverflow.com/a/13020494/2226796

+0

謝謝我更新了答案。 – 2014-01-16 19:07:29

+0

很容易爲我工作,謝謝! – 2015-12-10 07:08:10

+0

我如何檢查我是否已經登錄? (跳過發送登錄請求) – user3383675 2016-02-04 10:32:41

9

這是我如何ImpressPages解決了這個:

//initial request with login data 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php'); 
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36'); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts 
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts 
$answer = curl_exec($ch); 
if (curl_error($ch)) { 
    echo curl_error($ch); 
} 

//another request preserving the session 

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile'); 
curl_setopt($ch, CURLOPT_POST, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, ""); 
$answer = curl_exec($ch); 
if (curl_error($ch)) { 
    echo curl_error($ch); 
} 
+0

//由於某些網站在第二頁重定向並設置了cookie,因此可能需要: curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1); – 2014-11-15 22:54:38

0

巴拿馬傑克例不適合我的工作 - 給致命錯誤:調用未定義功能build_unique_path()。我用這個代碼 - (更簡單 - 我的意見):


// options
$login_email = '[email protected]';
$login_pass = 'alabala4807';
$cookie_file_path = "/tmp/cookies.txt";
$LOGINURL = "http://alabala.com/index.php?route=account/login";
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";

// begin script
$ch = curl_init();

// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";

// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

// set first URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);

// execute session to get cookies and required form inputs
$content = curl_exec($ch);

// grab the hidden inputs from the form required to login
$fields = getFormFields($content);
$fields['email'] = $login_email;
$fields['password'] = $login_pass;

// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($fields);
// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);

// set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);

// perform login
$result = curl_exec($ch);

print $result;

function getFormFields($data)
{
if (preg_match('/()/is', $data, $matches)) {
$inputs = getInputs($matches[1]);

return $inputs;
} else {
die('didnt find login form');
}
}

function getInputs($form)
{
$inputs = array();
$elements = preg_match_all("/(]+>)/is", $form, $matches);
if ($elements > 0) {
for($i = 0;$i $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];

$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}

$inputs[$name] = $value;
}
}
}

return $inputs;
}

$grab_url='http://grab.url/alabala';

//page with the content I want to grab
curl_setopt($ch, CURLOPT_URL, $grab_url);
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
curl_close($ch);

var_dump($html);
die;

+0

**我的回答不是複製和粘貼**。這只是我用過的一個例子。唯一相關的部分是** curl **代碼。 'build_unique_path'是我創建的一個自定義函數,我在該示例中註釋到了這一點。答案是創建自己的指南。 – 2017-07-08 07:34:26

+0

好的男人,不要判斷你,只是你的榜樣不適合我,沒有什麼個人的,我只是分享一個適合我的例子。是的,這不是我的代碼,但我的工作是測試和格式化示例 – Pavel 2017-07-10 12:34:26