php curl post登录随笔

Posted 在下刘彦直

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php curl post登录随笔相关的知识,希望对你有一定的参考价值。

研究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








































以上是关于php curl post登录随笔的主要内容,如果未能解决你的问题,请参考以下文章

PHP通过CURL模拟登录并获取数据

PHP CURL模拟登陆

使用curl进行模拟登录

php教程 CURL实现带有验证码网站的模拟登录的方法

使用 curl 登录到 WS1

PHP里模拟Post提交是啥意思?