php curl post登录与带cookie模拟登录随笔

研究curl  post登录

一步一步来,一个星期前我也不知道curl是啥。都是百度学习的,好了贴代码

这个是没有验证码的登录(账号密码自己申请一个)

<?php
$cookieSuccess = dirname(__FILE__)."/1769.tmp";
$ch = curl_init();
// 用户名\密码
$user = "ha王";
$pass = "asd1asd123";
$questionid=array('value'=>0);
$url = "http://www.mala.cn/member.php?mod=logging&action=login";
// 返回结果存放在变量中,不输出
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieVerify);
//curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
$fields_post = array("username"=> $user, "password"=> $pass,'value'=>0,"loginsubmit" => "Submit");
$headers_login = array("User-Agent" => "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
$fields_string = "";
foreach($fields_post as $key => $value){
$fields_string .= $key . "=" . $value . "&";
}
$fields_string = rtrim($fields_string , "&");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieSuccess);//用来存放登录成功的cookie

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result= curl_exec($ch);
curl_close($ch);
echo $result;

 

下面是cookie模拟登录

------------------------------------------------------------------------------------------------

$url = "http://www.mala.cn/thread-14378684-1-1.html";
$cookieSuccess = dirname(__FILE__)."/1769.tmp";
function request_url_data($url)
{
global $cookieSuccess;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieSuccess); //使用上面获取的cookies
$data=curl_exec($ch);
curl_close($ch);
return $data;
}

echo request_url_data($url);

--------------------------------------------------------------------------------------------------------------

cuir更多资料:http://www.android100.org/html/201406/28/32637.html

       http://www.tuicool.com/articles/3u2MNf

       https://segmentfault.com/q/1010000006103392

转载于:https://www.cnblogs.com/paddygege/p/6531719.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值