登录然后POST


Login Then POST

是否可以创建一个php代码来使用post登录并存储cookie?然后使用cookie做另一个帖子?

如果可能的话,我想要一个不显示任何内容的php代码,也许echo Post 1 OK echo Post 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);
?>

我强烈建议您了解网站是否有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);