相思资源网 Design By www.200059.com

使用curl之前先打开curl配置,具体方式百度一下就知道,开启curl扩展。密码用md5加密,这是经过测试成功的,把用户跟密码改成你的就行了。

下面一段代码给大家介绍php使用curl模拟登录微信公众平台,具体代码如下所示:

<"********");
$data = "f=json&imgcode=&pwd=$pwd&username=*****@***.com"; 
$ch = curl_init($login_url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch,CURLOPT_POST,1); 
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); 
curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com'); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$data); 
$content = curl_exec($ch); 
curl_close($ch); 
$newurl = json_decode($content,1); 
//var_dump($newurl);
//exit;
$newurl = $newurl['redirect_url']; 
//获取登入后页面的源码 
$go_url = 'https://mp.weixin.qq.com'.$newurl; 
$ch = curl_init($go_url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file); 
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$content = curl_exec($ch); 
//var_dump(curl_error($ch)); 
print_r($content); 
curl_close($ch); 
"color: #ff0000">使用 PHP CURL 模拟登录新浪微博

有时候我们获取一些新浪微博的数据,但又不想使用API,只好使用模拟登录了.

发现以前可以使用的CURL模拟登录代码失效了,Google一下,发现有很多人碰到这个问题.但是没有找到解决方法,所以就自己研究了一下,发现了原因.

可能是因为新浪限制了不允许模拟登录,同样的登录参数,用网页登录一切正常,用CURL登录,返回的COOKIES竟然是临时的.

所以看起来是登录成功了,并且获取到了用户信息,但是再次访问还是未登录状态.我的解决方法比较简单,直接修改COOKIES的时效这样就行了.

附上我自己测试通过的PHP代码如下,希望有对有同样问题的朋友有用,如果你有更好的方案欢迎分享一下.

发现只要不设置CURLOPT_COOKIESESSION参数就行了,不需要修改COOKIE_FILE.

<"http://weibo.com/logout.php");
unset($this->status);
}
/*不需要了,只要不设置HTTP函数中不设置CURLOPT_COOKIESESSION参数就行了,要设可以设为false.
function ResetCookie()//重置相关cookie
{
global $cookie_file;
$str = file_get_contents($cookie_file);
$t = time()+3600;//设置cookie有效时间一个小时
$str = preg_replace("/\t0\t/", "\t".$t."\t", $str);
$f = fopen($cookie_file,"w");
fwrite($f,$str);
fclose($f);
}
*/
function login($su,$sp,$flags = 0)
{
$su = urlencode(base64_encode($su));
$data = http("http://login.sina.com.cn/sso/prelogin.php".$su);
if (empty($data))
return null;
//$data = substr($data,35,-1);
$data = json_decode($data);
if ($data->retcode != 0)
return null;
if ($flags == 0)
$sp = sha1(sha1($sp));
$sp .= strval($data->servertime).$data->nonce;
$sp = sha1($sp);
$data = "url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3F&returntype=META&ssosimplelogin=1&su=".$su.'&service=miniblog&servertime='.$data->servertime."&nonce=".$data->nonce.'&pwencode=wsse&sp='.$sp;
$data = http("http://login.sina.com.cn/sso/login.php",$data);
//$this->ResetCookie();
if (preg_match("/location\.replace\('(.*)'\)/",$data,$url))
{
$data = http($url[1]);
//$this->ResetCookie();
$data = json_decode(substr($data,1,-2));
if ($data->result == true)
return $data->userinfo;
}
return null;
}
}
?>

以上内容给大家介绍了PHP Curl模拟登录微信公众平台、新浪微博实例代码,希望本文所述对大家有所帮助。

标签:
php_curl模拟登录,微信公众平台curl,新浪微博模拟登陆

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“PHP Curl模拟登录微信公众平台、新浪微博实例代码”

暂无PHP Curl模拟登录微信公众平台、新浪微博实例代码的评论...