2012-03-16 100 views
-1

它可能創建一個php代碼來登錄使用後和存儲cookie? 然後使用cookies做另一個帖子?登錄然後POST

如果可能的話我想沒有表現出任何一個PHP代碼,也許後回聲1 OK後回聲2 OK

我發現這個代碼,但它不工作

<?php 
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, 'http://xxxxxxxx/login.php'); 
curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=xxxxxxx&password=xxxxxxxxx&submit=Login'); 
Curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
$store = Curl_exec ($ch); 
curl_setopt ($ch. CURLOPT_URL, 'http://xxxxxx/postlink2.php'); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'info1=xxxxxxx&info2=xxxxxx&info3=xxxxxxxxxx'); 
$content = curl_exec ($ch); 
curl_close ($ch); 
?> 

回答

0

我會強烈建議找如果網站有一個API可以用來代替這條路線,但是,這是可能的。下面應該工作,雖然我沒有測試它:

$curl = curl_init('http://xxxxxxxx/login.php'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HEADER, true); 
curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, 'username=xxxxxxx&password=xxxxxxxxx&submit=Login'); 
$response = curl_exec($curl); 
preg_match('/^Set-Cookie: ([^;]*)/m', $response, $matches); 
$cookie = $matches[1]; 
curl_close($curl); 

$secondRequest = curl_init('http://xxxxxx/postlink2.php'); 
curl_setopt($secondRequest, CURLOPT_POSTFIELDS, 'info1=xxxxxxx&info2=xxxxxx&info3=xxxxxxxxxx'); 
curl_setopt($secondRequest, CURLOPT_COOKIE, $cookie); 
curl_setopt($secondRequest, CURLOPT_RETURNTRANSFER, true); 
$content = curl_exec($secondRequest); 
curl_close($secondRequest); 
+0

我剛測試這個腳本,但第二個請求dnt的工作。 – Gorfi 2012-03-17 00:28:48