使用 cURL(或更好的东西)提交 HTTPS 表单
function login_board($url,$ch)
{
$cookie="cookie.txt";
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'user_id=myid&password=mypass&action
=login');
curl_setopt ($ch, CURLOPT_POST, 1);
echo $content = curl_exec( $ch );
}
以下方法不会让我登录该网站。如果我连续运行两次,它只会再次显示登录页面,而不是显示欢迎屏幕。
如果我尝试访问其中的页面,它会给我一个错误的请求。
function login_board($url,$ch)
{
$cookie="cookie.txt";
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'user_id=myid&password=mypass&action
=login');
curl_setopt ($ch, CURLOPT_POST, 1);
echo $content = curl_exec( $ch );
}
The following approach doesn't log me into the site. If I run this twice consecutively, it just shows me the login page again instead of showing me the welcome screen.
If I try to access a page inside, it gives me a bad request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cookie jar 只会保存 cookie,但是在下一个请求中,您还需要发回 cookie .. 查看 CURLOPT_COOKIEFILE
cookie jar will only save the cookie, however on the next requests u need to send back the cookie as well .. look into CURLOPT_COOKIEFILE