php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
本文實例講述了php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
php CURL函數(shù)可以模仿用戶進(jìn)行一些操作,如我們可以模仿用戶提交數(shù)據(jù)也可以模仿用戶進(jìn)行網(wǎng)站訪問了,下面我們來介紹利用CURL模擬進(jìn)行微信接口的GET與POST例子,例子非常的簡單就兩個:
Get提交獲取數(shù)據(jù)
/** * @desc 獲取access_token * @return String access_token */ function getAccessToken(){ $AppId = '1232assad13213123'; $AppSecret = '2312312321adss3123213'; $getUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $getUrl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURL_SSLVERSION_SSL, 2); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); $response = json_decode($data); return $response->access_token; }
post提交獲取數(shù)據(jù)
/** * @desc 實現(xiàn)天氣內(nèi)容回復(fù) */ public function testWeixin(){ $access_token = $this->getAccessToken(); $customMessageSendUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token; $description = '今天天氣的詳細(xì)信息(從第三方獲取)。'; $url = 'http://weather.com/'; $picurl = 'http://weather.com/'; $postDataArr = array( 'touser'=>'OPENID', 'msgtype'=>'news', 'news'=>array( 'articles'=>array( 'title'=>'當(dāng)天天氣', 'description'=>$description, 'url'=>$url, 'picurl'=>$picurl, ), ), ); $postJosnData = json_encode($postDataArr); $ch = curl_init($customMessageSendUrl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); var_dump($data); }
例子相對來說比較簡單也沒有什么好詳細(xì)分析的了,大家照抄就可以實現(xiàn)我們想要的功能了.
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP中使用cURL實現(xiàn)Get和Post請求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php的curl實現(xiàn)get和post的代碼
- PHP中的使用curl發(fā)送請求(GET請求和POST請求)
- PHP的curl實現(xiàn)get,post和cookie(實例介紹)
- 詳解php用curl調(diào)用接口方法,get和post兩種方式
- PHP CURL模擬GET及POST函數(shù)代碼
- PHP如何使用cURL實現(xiàn)Get和Post請求
- PHP中使用CURL發(fā)送get/post請求上傳圖片批處理功能
- php curl發(fā)起get與post網(wǎng)絡(luò)請求案例詳解
- PHP curl get post 請求的封裝函數(shù)示例【get、post、put、delete等請求類型】
相關(guān)文章
淺談PHP中靜態(tài)方法和非靜態(tài)方法的相互調(diào)用
下面小編就為大家?guī)硪黄獪\談PHP中靜態(tài)方法和非靜態(tài)方法的相互調(diào)用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10laravel5.6 框架郵件隊列database驅(qū)動簡單demo示例
這篇文章主要介紹了laravel5.6 框架郵件隊列database驅(qū)動,結(jié)合實例形式詳細(xì)分析了laravel5.6郵件隊列database驅(qū)動具體設(shè)置、實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2020-01-01