欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP Curl模擬登錄微信公眾平臺、新浪微博實例代碼

 更新時間:2016年01月28日 09:11:39   作者:得了藤蔓  
這篇文章主要介紹了PHP Curl模擬登錄微信公眾平臺、新浪微博實例代碼的相關(guān)資料,涉及到php curl模擬登錄相關(guān)知識,需要的朋友可以參考下

使用curl之前先打開curl配置,具體方式百度一下就知道,開啟curl擴(kuò)展。密碼用md5加密,這是經(jīng)過測試成功的,把用戶跟密碼改成你的就行了。

下面一段代碼給大家介紹php使用curl模擬登錄微信公眾平臺,具體代碼如下所示:

<?php 
//模擬微信登入 
$cookie_file = tempnam('./temp','cookie'); 
$login_url = 'https://mp.weixin.qq.com/cgi-bin/login'; 
$pwd = md5("********");
$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); 
?>

使用 PHP CURL 模擬登錄新浪微博

有時候我們獲取一些新浪微博的數(shù)據(jù),但又不想使用API,只好使用模擬登錄了.

發(fā)現(xiàn)以前可以使用的CURL模擬登錄代碼失效了,Google一下,發(fā)現(xiàn)有很多人碰到這個問題.但是沒有找到解決方法,所以就自己研究了一下,發(fā)現(xiàn)了原因.

可能是因為新浪限制了不允許模擬登錄,同樣的登錄參數(shù),用網(wǎng)頁登錄一切正常,用CURL登錄,返回的COOKIES竟然是臨時的.

所以看起來是登錄成功了,并且獲取到了用戶信息,但是再次訪問還是未登錄狀態(tài).我的解決方法比較簡單,直接修改COOKIES的時效這樣就行了.

附上我自己測試通過的PHP代碼如下,希望有對有同樣問題的朋友有用,如果你有更好的方案歡迎分享一下.

發(fā)現(xiàn)只要不設(shè)置CURLOPT_COOKIESESSION參數(shù)就行了,不需要修改COOKIE_FILE.

<?php
class sina
{
/*
一個簡單的新浪微搏curl模擬登錄類. 來源: http://chenall.net/post/sina_curl_login/
使用方法:
http函數(shù)是一個簡單的curl封裝函數(shù),需要自己去實現(xiàn),
http函數(shù)原型如下:
http($url,$post_data = null)
返回網(wǎng)頁內(nèi)容.
第一個參數(shù)$url,就是要訪問的url地址,$post_data是post數(shù)據(jù),如果為空,則代表GET訪問.
1.使用加密后密碼登錄 加密方法: sha1(sha1($pass))
$sina = new sina($username,$sha1pass)
2.直接使用原始密碼登錄
$sina = new sina($username,$sha1pass,0)
執(zhí)行之后如果$sina->status非空,則登錄成功,否則登錄失敗.
登錄成功之后,你就可以直接繼續(xù)使用http函數(shù)來訪問其它內(nèi)容.
使用 unset($sina) 會自動注銷登錄.
*/
public $status;
function __construct($su,$sp,$flags = 1) {
$this->status = $this->login($su,$sp,$flags);
}
function __destruct()
{
//注銷登錄
$this->logout();
}
function logout()
{
http("http://weibo.com/logout.php");
unset($this->status);
}
/*不需要了,只要不設(shè)置HTTP函數(shù)中不設(shè)置CURLOPT_COOKIESESSION參數(shù)就行了,要設(shè)可以設(shè)為false.
function ResetCookie()//重置相關(guān)cookie
{
global $cookie_file;
$str = file_get_contents($cookie_file);
$t = time()+3600;//設(shè)置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?entry=miniblog&client=ssologin.js&user=".$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?client=ssologin.js",$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;
}
}
?>

以上內(nèi)容給大家介紹了PHP Curl模擬登錄微信公眾平臺、新浪微博實例代碼,希望本文所述對大家有所幫助。

相關(guān)文章

最新評論