CURL: PHP : can't submit


CURL: PHP : can't submit

我需要编写一个php脚本,该脚本将登录到我的管理页面,然后提交rss。我可以用下面的代码登录,但不能提交rss

 <?php
function rssadd($url,$post,$post2) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.4) Gecko/2008102920 AdCentriaIM/1.7 Firefox/3.0.4');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.4) Gecko/2008102920 AdCentriaIM/1.7 Firefox/3.0.4');
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $post2);
curl_setopt($ch2, CURLOPT_REFERER, $url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$result2 = curl_exec($ch2);
return $result . $result2;
}
    $page2 = rssadd('http://site.com/admin.php?mod=rss&action=news&id=4','subaction=dologin&username=admin&password=pass','subaction=doit');
echo $page2;
?>

上的htmlhttp://site.com/admin.php?mod=rss&action=新闻&id=4"我无法提交

 <input type="submit" name="subaction" value="doit" class="buttons">

也许您需要在请求之间保留cookie?尝试将CURLOPT_COOKIEFILE设置为''(空字符串)。您还需要在两个请求上使用相同的curl句柄,而不是关闭第一个句柄并初始化新的句柄,只需更改第一个句柄的选项并再次运行即可。

我最初是从这个Stack Overflow答案中学到如何做到这一点的:https://stackoverflow.com/a/5758471/638544