PHP Curl模擬登錄微信公眾平臺、新浪微博實(shí)例代碼
使用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í)候我們獲取一些新浪微博的數(shù)據(jù),但又不想使用API,只好使用模擬登錄了.
發(fā)現(xiàn)以前可以使用的CURL模擬登錄代碼失效了,Google一下,發(fā)現(xiàn)有很多人碰到這個(gè)問題.但是沒有找到解決方法,所以就自己研究了一下,發(fā)現(xiàn)了原因.
可能是因?yàn)樾吕讼拗屏瞬辉试S模擬登錄,同樣的登錄參數(shù),用網(wǎng)頁登錄一切正常,用CURL登錄,返回的COOKIES竟然是臨時(shí)的.
所以看起來是登錄成功了,并且獲取到了用戶信息,但是再次訪問還是未登錄狀態(tài).我的解決方法比較簡單,直接修改COOKIES的時(shí)效這樣就行了.
附上我自己測試通過的PHP代碼如下,希望有對有同樣問題的朋友有用,如果你有更好的方案歡迎分享一下.
發(fā)現(xiàn)只要不設(shè)置CURLOPT_COOKIESESSION參數(shù)就行了,不需要修改COOKIE_FILE.
<?php
class sina
{
/*
一個(gè)簡單的新浪微搏curl模擬登錄類. 來源: http://chenall.net/post/sina_curl_login/
使用方法:
http函數(shù)是一個(gè)簡單的curl封裝函數(shù),需要自己去實(shí)現(xiàn),
http函數(shù)原型如下:
http($url,$post_data = null)
返回網(wǎng)頁內(nèi)容.
第一個(gè)參數(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有效時(shí)間一個(gè)小時(shí)
$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模擬登錄微信公眾平臺、新浪微博實(shí)例代碼,希望本文所述對大家有所幫助。
- PHP實(shí)現(xiàn)的微信公眾號掃碼模擬登錄功能示例
- PHP使用Curl實(shí)現(xiàn)模擬登錄及抓取數(shù)據(jù)功能示例
- php curl 模擬登錄并獲取數(shù)據(jù)實(shí)例詳解
- PHP curl模擬登錄帶驗(yàn)證碼的網(wǎng)站
- PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法
- PHP使用CURL實(shí)現(xiàn)對帶有驗(yàn)證碼的網(wǎng)站進(jìn)行模擬登錄的方法
- PHP中使用CURL模擬登錄并獲取數(shù)據(jù)實(shí)例
- PHP CURL獲取cookies模擬登錄的方法
- PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄
相關(guān)文章
PHP中類靜態(tài)調(diào)用和范圍解析操作符的區(qū)別解析
這篇文章主要介紹了PHP中類靜態(tài)調(diào)用和范圍解析操作符的區(qū)別,需要的朋友可以參考下2018-01-01
PHP框架實(shí)現(xiàn)WebSocket在線聊天通訊系統(tǒng)
這篇文章主要介紹了PHP框架結(jié)合實(shí)現(xiàn)WebSocket在線聊天通訊系統(tǒng),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
PHPStudy hosts文件可能不存在或被阻止打開及同步hosts失敗問題
這篇文章主要介紹了PHPStudy hosts文件可能不存在或被阻止打開,同步hosts失敗,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
淺析THINKPHP的addAll支持的最大數(shù)據(jù)量
這篇文章主要介紹了THINKPHP的addAll支持的最大數(shù)據(jù)量,需要的朋友可以參考下2015-02-02
discuz 首頁四格:最新話題+最新回復(fù)+熱門話題+精華文章插件
discuz 首頁四格:最新話題+最新回復(fù)+熱門話題+精華文章插件...2007-08-08
Thinkphp5 自定義上傳文件名的實(shí)現(xiàn)方法
這篇文章主要介紹了Thinkphp5 自定義上傳文件名的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07

