導航:首頁 > 編程語言 > php獲取微信token

php獲取微信token

發布時間:2022-07-23 17:40:16

『壹』 php獲取微信token的是什麼格式的

access_token是公眾號的全局唯一票據,公眾號調用各介面時都需使用access_token。開發者需要進行妥善保存。
access_token的存儲至少要保留512個字元空間。access_token的有效期目前為2個小時,需定時刷新,重復獲取將導致上次獲取的
access_token失效。
access_token的獲取:
<?php

define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");

$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url); //獲取文件內容或獲取網路請求的內容
//echo $res;
$result = json_decode($res, true); //接受一個 JSON 格式的字元串並且把它轉換為 PHP 變數
$access_token = $result['access_token'];
echo $access_token;

php>

『貳』 PHP獲取 微信access token 連接被重置

可能你的代碼涉及到了php5.3的新增特性~
比如5.3以上版本才支持 命名空間namespace,但5.3以下版本是不支持的,建議安裝相同的伺服器環境

『叄』 PHP和微信平台Token問題,求解 很簡單。

如果程序沒問題,一般情況是編碼問題!

『肆』 PHP如何實現微信網頁授權

具體而言,網頁授權流程分為四步:
1、引導用戶進入授權頁面同意授權,獲取code
2、通過code換取網頁授權access_token(與基礎支持中的access_token不同)
3、如果需要,開發者可以刷新網頁授權access_token,避免過期
4、通過網頁授權access_token和openid獲取用戶基本信息(支持unionid機制)

『伍』 微信 在php中 如何獲取access_token

access_token是公眾號的全局唯一票據,公眾號調用各介面時都需使用access_token。開發者需要進行妥善保存。
access_token的存儲至少要保留512個字元空間。access_token的有效期目前為2個小時,需定時刷新,重復獲取將導致上次獲取的
access_token失效。

access_token的獲取:

<?php

define("APPID","您的appid");
define("APPSECRET","您的appsecret");

$token_access_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
$res=file_get_contents($token_access_url);//獲取文件內容或獲取網路請求的內容
//echo$res;
$result=json_decode($res,true);//接受一個JSON格式的字元串並且把它轉換為PHP變數
$access_token=$result['access_token'];
echo$access_token;

php>

『陸』 PHP的TP框架,使用curl函數無法通過微信獲取access_token的那個https的鏈接,設置curl參數也不行

先看下直接用curl命令是否能正常訪問,然後打開curl的調試,看看具體輸出。

『柒』 PHP 微信模擬登錄獲取不到token

已經認證的服務號被檢測到模擬登錄,強制手機(phone=+86158******71 後面的就是被掩碼的手機號)開啟簡訊驗證。只有當真正瀏覽器時就會通過JS觸發簡訊通知,模擬登陸則不會。

在頁面輸入收到的簡訊驗證碼後,才能獲得 登錄token。

這是微信官方的技術手段,此問題暫時無解。

『捌』 Thinkphp3.2.3接入公眾號響應微信發送的Token驗證總是失敗

用得著判斷那個TOKEN?是不是多此一舉,用我這個,包你滿意

<?php
namespaceHomeController;
useThinkController;
{
publicfunctionindex(){
$v=$_GET["echostr"];
if(!isset($v)){//根據隨機字元串判斷是否之前已經驗證過,避免重復驗證
$this->receive();
}else{
$this->checkSignature();
}
}
//未驗證執行驗證
publicfunctioncheckSignature(){
$signature=I("signature");//加密簽名
$timestamp=I("timestamp");//時間戳
$nonce=I("nonce");//隨機數
$token=TOKEN;//token
$tmpArr=array($token,$timestamp,$nonce);//組成新數組
sort($tmpArr,SORT_STRING);//重新排序
$tmpStr=implode($tmpArr);//轉換成字元串
$tmpStr=sha1($tmpStr);//再將字元串進行加密
if($tmpStr==$signature)
{
echo$_GET["echostr"];
exit;
//returntrue;
}else{
returnfalse;
}
}
//已經驗證過執行這里
publicfunctionreceive(){
$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];//獲取用戶發送的消息,格式為xml
//$postStr=file_get_contents("php://input");
libxml_disable_entity_loader(true);
$postObj=simplexml_load_string($postStr,"SimpleXMLElement",LIBXML_NOCDATA);
$this->logger($postStr);
//並且回復信息給用戶
if(!empty($postObj)){
switch($postObj->MsgType){
case"text":
$result=$this->receiveText($postObj);
//避免錯誤信息報出
if(empty($result)){
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),$postObj->MsgType,"多圖文信息沒有獲取到");
}
echo$result;
break;
case"image":
$result=$this->receiveImage($postObj);
if(empty($result)){
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"news","跟老李一起來做微信開發","微信開發即微信公眾平台開發,將企業信息、服務、活動等內容通過微信網頁的方式進行表現,用戶通過簡單的設置,就能生成微信3G網站。通過微信公眾平台將企業品牌展示給微信用戶,減少宣傳成本,建立企業與消費者、客戶的一對一互動和溝通,將消費者接入企業CRM系統,進行促銷、推廣、宣傳、售後等。形成了一種主流的線上線下微信互動營銷方式。","https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/ke/w%3D268%3Bg%3D0/sign=/.jpg","http://www..com");
}
echo$result;
break;
case"event":
$result=$this->receiveEvent($postObj);
if(empty($result)){
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","感謝您關注微信介面測試服務號,我是李先生!");
}
echo$result;
break;
default:
$result="你發送的數據類型暫時不做處理:".$postObj->MsgType;
echo$result;
break;
}
}

}
//log
privatefunctionlogger($content){
$logSize=100000;
$log="./log.txt";
if(file_exists($log)&&filesize($log)>$logSize){
unlink($log);
}
file_put_contents($log,date("Y-m-dH:i:s")."".$content." ",FILE_APPEND);
}
//關注自動回復信息
privatefunctionreceiveEvent($postObj){
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
if($postObj->Event=="subscribe"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","感謝您關注微信介面測試服務號,我是李先生!");
}elseif($postObj->Event=="CLICK"){
if($postObj->EventKey=="getSalary"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【查詢->工資】按鈕");
}elseif($postObj->EventKey=="getAttendance"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【查詢->考勤記錄】按鈕");
}elseif($postObj->EventKey=="getBonus"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【查詢->獎金明細】按鈕");
}elseif($postObj->EventKey=="getAbout"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【關於我們->聯系】按鈕");
}elseif($postObj->EventKey=="getJoin"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【關於我們->加入我們】按鈕");
}elseif($postObj->EventKey=="getCustomer"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【關於我們->客服】按鈕");
}elseif($postObj->EventKey=="getInternational"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【新聞->國際】按鈕");
}elseif($postObj->EventKey=="getInstry"){
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您點擊了【新聞->行業】按鈕");
}
}elseif($postObj->Event=="scancode_waitmsg"){
//掃碼帶提示的事件
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您使用了掃碼帶提示事件,事件KEY:{$postObj->EventKey}");
}elseif($postObj->Event=="scancode_push"){
//掃碼推事件
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您使用了掃碼推事件,事件KEY:{$postObj->EventKey}");
}else{
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"text","您的操作有點奇葩");
}
return$result;
}
//處理接收的文本信息並且回復給用戶
privatefunctionreceiveText($postObj){
$content=trim($postObj->Content);
//if(strstr($content,"你好")){
if(preg_match("/^d*$/",$content)){
//回復多圖文信息
$result=$this->receiveNews($postObj);
return$result;
}elseif($content=="圖片"){
//回復圖片信息
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"image","-");
return$result;
}else{
//回復文本信息
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),$postObj->MsgType,$content);
return$result;
}
}
//處理接收的文本信息並且回復給用戶為單圖文信息
privatefunctionreceiveImage($postObj){
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
$result=sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),"news","跟老李一起來做微信開發","微信開發即微信公眾平台開發,將企業信息、服務、活動等內容通過微信網頁的方式進行表現,用戶通過簡單的設置,就能生成微信3G網站。通過微信公眾平台將企業品牌展示給微信用戶,減少宣傳成本,建立企業與消費者、客戶的一對一互動和溝通,將消費者接入企業CRM系統,進行促銷、推廣、宣傳、售後等。形成了一種主流的線上線下微信互動營銷方式。","https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/ke/w%3D268%3Bg%3D0/sign=/.jpg","http://www..com");
return$result;
}
//處理接收的文本信息並且回復給用戶為多圖文信息
privatefunctionreceiveNews($postObj){
$con=array(
array("Title"=>"觀前街","Description"=>"hjhhj","PicUrl"=>"https://gss0..com/9fo3dSag_xI4khGko9WTAnF6hhy/image/pic/item/.jpg","Url"=>"https://gss0..com/9fo3dSag_xI4khGko9WTAnF6hhy/image/pic/item/.jpg"),
array("Title"=>"平江路","Description"=>"mkbkbjj","PicUrl"=>"http://imgtu.5011.net/uploads/content/shehui/wanxiang/2015-08-26/.jpg","Url"=>"http://imgtu.5011.net/uploads/content/shehui/wanxiang/2015-08-26/.jpg"),
array("Title"=>"觀前街","Description"=>"hjhhj","PicUrl"=>"http://img2.a0bi.com/upload/ttq/20160729/1469781927056.jpeg","Url"=>"http://img2.a0bi.com/upload/ttq/20160729/1469781927056.jpeg"),
array("Title"=>"平江路","Description"=>"mkbkbjj","PicUrl"=>"http://gaoxiao.zhutou.com/html/UploadPic/2010-3/201033173945493.jpg","Url"=>"http://gaoxiao.zhutou.com/html/UploadPic/2010-3/201033173945493.jpg"),
array("Title"=>"觀前街","Description"=>"hjhhj","PicUrl"=>"http://www.haha365.com/uploadfile/2013/0508/20130508102432191.jpg","Url"=>"http://www.haha365.com/uploadfile/2013/0508/20130508102432191.jpg"),
array("Title"=>"平江路","Description"=>"mkbkbjj","PicUrl"=>"http://img1.imgtn.bdimg.com/it/u=995757682,3656700920&fm=214&gp=0.jpg","Url"=>"http://img1.imgtn.bdimg.com/it/u=995757682,3656700920&fm=214&gp=0.jpg"),
array("Title"=>"觀前街","Description"=>"hjhhj","PicUrl"=>"http://down.laifu.com/tupian/201261510566.jpg","Url"=>"http://down.laifu.com/tupian/201261510566.jpg"),
array("Title"=>"平江路","Description"=>"mkbkbjj","PicUrl"=>"http://img.tupianzj.com/uploads/allimg/160528/9-16052Q54439-50.jpg","Url"=>"http://img.tupianzj.com/uploads/allimg/160528/9-16052Q54439-50.jpg")
);

$bodys="<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";

$tpl2="";
foreach($conas$key=>$newArray){
$tpl2.=sprintf($bodys,$newArray["Title"],$newArray["Description"],$newArray["PicUrl"],$newArray["Url"]);
}
$fromUsername=$postObj->FromUserName;
$toUsername=$postObj->ToUserName;
$tpl1="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>";
$tpl3="</Articles>
</xml>";


$tpl1=sprintf($tpl1,$fromUsername,$toUsername,time(),"news",count($con));
$result=$tpl1.$tpl2.$tpl3;
return$result;
}
}

『玖』 php 獲取微信獲取openid需要token嗎

如果是公眾號自動回復的功能,是不需要token就能獲取用戶openid,如果是網頁授權獲取,那麼就需要token了。微信默認token有效期2小時,所以你獲取到token後可以自行伺服器緩存2小時(稍微小於2小時最好),沒刷新一次就會重新獲取token,每天token的用量有限制。文檔有介紹

『拾』 自己用thinkphp做微信介面類文件,遇一個問題,GET獲取到的token,如何將這個變數傳遞到實例化的類裡面

classIndexActionextendsAction{

publicfunctionindex(){
$token=D("wx_token")->field("add_ip",true)->select();
foreach($tokenas$key=>$v){
if($v['expire_time']-300<time()){
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$v['appid']}&secret={$v['secret']}";
$res=file_get_contents($url,false);
$this->getAccessToken($v['id'],$res);

}else{
$date=date("Y-m-dH:i:s");
echo"{$date}_____noexpire ";
}
}
}

privatefunctiongetAccessToken($token,$res){
$sign=false;
$json=json_decode($res);
$data['id']=$token;
$data['access_token']=$json->access_token;
$data['expire_time']=time()+7200;
$data['add_time']=time();
$data['add_ip']='127.0.0.1';
$db=D("wx_token");
$db->startTrans();
if($db->save($data)){
$db->commit();
$sign=true;
}else{
$db->rollback();
$sign=false;
}
return$sign;
}

}

再創建個數據表,保存就好了,整個定時任務,定時去獲取比較好。

閱讀全文

與php獲取微信token相關的資料

熱點內容
歐洲cf玩什麼伺服器 瀏覽:525
如何連接另一台電腦上的共享文件夾 瀏覽:673
如何讓桌面文件夾搬家到e盤 瀏覽:69
java自動格式化 瀏覽:617
ipad怎麼查看文件夾大小 瀏覽:581
手工粘土解壓球 瀏覽:550
在線視頻教育源碼 瀏覽:39
快四十學什麼編程 瀏覽:754
gnumakelinux 瀏覽:537
視易峰雲伺服器怎麼改系統 瀏覽:535
javamap取值 瀏覽:768
mac和win磁碟加密軟體 瀏覽:474
蘋果為什麼會連接不到伺服器 瀏覽:726
pdf格式文件如何保存 瀏覽:303
小霸王伺服器tx什麼意思 瀏覽:75
解釋dns命令 瀏覽:584
dmx512怎麼編程 瀏覽:744
北京雲主機17t雲伺服器 瀏覽:232
php伺服器url地址 瀏覽:440
哪裡看書免費app 瀏覽:437