2016-03-13 175 views
1

我想使用捲曲登錄https://www.investagrams.com/login,它只是顯示空白頁。我新來捲曲順便說一句,所以請裸露在我身邊。有人可以指出我的代碼中存在什麼問題。https使用捲曲登錄

<!DOCTYPE html> 
<html> 
<body> 

<?php 

// Create temp file to store cookies 
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); 

// URL to login page 
$url = "https://www.investagrams.com/login"; 

// Get Login page and its cookies and save cookies in the temp file 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($ch); 

$fields = array(
'ctl00$WelcomePageMainContent$ctl00$Username' => '******', 
'ctl00$WelcomePageMainContent$ctl00$Password' => '******', 
); 

$fields_string = ''; 
foreach($fields as $key=>$value) { 
$fields_string .= $key . '=' . $value . '&'; 
} 
rtrim($fields_string, '&'); 

// Post login form and follow redirects 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, count($fields)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$output = curl_exec($ch); 


$url = "https://www.investagrams.com/Stock/RealTimeMonitoring"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($ch); 

?> 
</body> 
</html> 
+1

你是第一個url似乎缺少'.com' – Terminus

+0

你需要在第二次聲明後返回$ output的值。 –

+0

我嘗試刪除第一個$輸出並回顯第二個$輸出,但仍然返回空白頁@ C.Liddell – Newboy11

回答

2

有我注意到你的代碼錯誤的幾件事情:

  1. 你需要回到你的第二個捲曲請求的值。
  2. 如果您使用curl從已經包裹在這些標籤中的網站下拉信息,則不需要聲明DOCTYPE或使用<html><body>標籤。
  3. 由於@Terminus在對您的問題發表評論時提及,您需要在第一個網址的末尾加上.com

這些修改後的代碼應該是:

<?php 

// Create temp file to store cookies 
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); 

// URL to login page 
$url = "https://www.investagrams.com/login"; 

// Get Login page and its cookies and save cookies in the temp file 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Accepts all CAs 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($ch); 

$fields = array(
'ctl00$WelcomePageMainContent$ctl00$Username' => '******', 
'ctl00$WelcomePageMainContent$ctl00$Password' => '******', 
); 

$fields_string = ''; 
foreach($fields as $key=>$value) { 
$fields_string .= $key . '=' . $value . '&'; 
} 
rtrim($fields_string, '&'); 

// Post login form and follow redirects 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Accepts all CAs 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, count($fields)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$output = curl_exec($ch); 

$url = "https://www.investagrams.com/Stock/RealTimeMonitoring"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Accepts all CAs 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($ch); 
echo $output; 

?> 

編輯:

正如@湯姆在下方留言,換言之,你不應該使用curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);,除非絕對必要,會發生什麼是它不不驗證由服務器提供的SSL證書,以允許模擬任何捲曲連接到的服務器。

此外,由於https://investagrams.com具有有效的SSL證書,因此沒有理由將CURLOPT_SSL_VERIFYPEER設置爲false。

+0

哇!它的作品兄弟謝謝你。你爲我節省了很多時間 – Newboy11

+0

@ Newboy11很高興我能幫上忙。 –

+0

@ C.Liddell您應該添加關於CURLOPT_SSL_VERIFYPEER的警告,因爲它具有嚴重的安全隱患。 – Tom