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)文章
Laravel 在views中加載公共頁面的實現(xiàn)代碼
今天小編就為大家分享一篇Laravel 在views中加載公共頁面的實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP+Ajax異步通訊實現(xiàn)用戶名郵箱驗證是否已注冊( 2種方法實現(xiàn))
在網(wǎng)站注冊用戶時使用,主要為了無刷新異步驗證用戶輸入的用戶名或者Email是否已注冊。2011-12-12
使用PHP備份MySQL和網(wǎng)站發(fā)送到郵箱實例代碼
這篇文章主要介紹了使用PHP備份MySQL和網(wǎng)站發(fā)送到郵箱的方法,大家參考使用吧2013-11-11
PHP命名空間(namespace)的動態(tài)訪問及使用技巧
上篇文章介紹了PHP命名空間的一些術(shù)語,其解析規(guī)則,本文我們來繼續(xù)講述PHP命名空間動態(tài)訪問及使用技巧,希望能有所幫助2014-08-08

