《PHP实例: php运用CURL去模拟GET以及POST朝着微信接口递交并且获取数据的途径》要点:
关于PHP实例, 此实例是为展示经由PHP使用CURL来以模拟GET以及POST的方式去向微信接口递交以及获取数据, 从而期望能对您产生用途。假设出现疑问, 那么就可以与我们取得联系。
这里有一个关于PHP的实战内容, 此内容中的实例阐述了, 运用PHP借助CURL去模拟GET或者POST, 进而朝着微信接口提交数据以及获取数据所采用的办法, 并且将其分享给众人给予大家当作参考,具体的情况是这样的:
用户提交过的数据可被PHP实战里的php CURL函数模仿进行一些操作, 像模仿用户提交数据, 或者模仿用户进行网站访问, 接下来要介绍利用CURL模拟进行微信接口的GET与POST例子, 例子极为简单, 仅有两个。
PHP实战Get提交获取数据
PHP实战 /** * @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; }/** * @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; }PHP实战post提交获取数据
PHP实战 /** * @desc 实现天气内容回复 */ public function testWeixin(){ $access_token = $this->getAccessToken(); $customMessageSendUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token; $description = '今天天气的详细信息(从第三方获取).'; $url = 'http://weather.com/'; $picurl = 'http://weather.com/'; $postDataArr = array( 'touser'=>'OPENID', 'msgtype'=>'news', 'news'=>array( 'articles'=>array( 'title'=>'当天天气', '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); }/** * @desc 实现天气内容回复 */ public function testWeixin(){ $access_token = $this->getAccessToken(); $customMessageSendUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token; $description = '今天天气的详细信息(从第三方获取).'; $url = 'http://weather.com/'; $picurl = 'http://weather.com/'; $postDataArr = array( 'touser'=>'OPENID', 'msgtype'=>'news', 'news'=>array( 'articles'=>array( 'title'=>'当天天气', '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); }PHP实战的例子, 相对而言是比较简单的, 并且也不存在什么值得详细去分析的内容了, 大家按照原样去抄写, 便能够实现我们所希望达成的功能了。
诸多对PHP实战, 以及更多关于PHP相关内容有着浓厚兴趣的读者, 能够去查看本站专题, 其中包括《PHP微信开发技巧汇总》, 还有《PHP编码与转码操作技巧汇总》, 以及《PHP网络编程技巧总结》, 再者《PHP基本语法入门教程》, 另外《php字符串()用法总结》, 并且《php+mysql数据库操作入门教程》, 以及《php常见数据库操作技巧汇总》。
PHP实战希望本文所述对大家PHP程序设计有所帮助.