2012-03-16 141 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