PHP Curl模擬登錄微信公眾平臺、新浪微博實例代碼
使用curl之前先打開curl配置,具體方式百度一下就知道,開啟curl擴展。密碼用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)文章
PHP 實現(xiàn)字符串翻轉(zhuǎn)(包含中文漢字)的實現(xiàn)代碼
本篇文章主要介紹了PHP 實現(xiàn)字符串翻轉(zhuǎn)(包含中文漢字)的實現(xiàn)代碼,在PHP面試題中經(jīng)常遇到,有興趣的可以了解一下。2017-04-04
php小型企業(yè)庫存管理系統(tǒng)的設(shè)計與實現(xiàn)代碼
雙專業(yè)畢業(yè)論文做了一個小型企業(yè)庫存管理系統(tǒng),使用PHP&mysql組合進行開發(fā)。頭一次開發(fā)稍微大一點的東西,php也是自學(xué)的2011-05-05
php筆記之:php數(shù)組相關(guān)函數(shù)的使用
本篇文章介紹了,php中數(shù)組相關(guān)函數(shù)的使用。需要的朋友參考下2013-04-04
Zend Framework數(shù)據(jù)庫操作技巧總結(jié)
這篇文章主要介紹了Zend Framework數(shù)據(jù)庫操作技巧,結(jié)合實例形式總結(jié)分析了Zend Framework針對數(shù)據(jù)庫操作的常見函數(shù)、常用操作及相關(guān)注意事項,需要的朋友可以參考下2017-02-02
Laravel 數(shù)據(jù)庫加密及數(shù)據(jù)庫表前綴配置方法
今天小編就為大家分享一篇Laravel 數(shù)據(jù)庫加密及數(shù)據(jù)庫表前綴配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHPStorm2020.1永久激活及下載更新至2020(推薦)
這篇文章主要介紹了PHPStorm2020.1永久激活及下載更新至2020,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

