zoukankan      html  css  js  c++  java
  • 微信接口开发1--向微信发送请求--获取access_token

    //随便放置一个php文件在服务器上.执行该方法--调用模拟get提交---到微信-->获得微信返回的access_token

    不建议自己编写模拟get提交方法.

    建议直接导入微信框架LaneWeChat 中的curl.lib.php文件

    01====================直接使用lanewechat框架中的模拟get提交

    getaccess_tokyn.php文件

    <?php
    include './curl.lib.php';

    define("WECHAT_APPID", 'wx7185adb8b784b701');
    define("WECHAT_APPSECRET", '952524d16b184f93beef7ead9366a549');

    function _getAccessToken(){
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.WECHAT_APPID.'&secret='.WECHAT_APPSECRET;

    $accessToken = Curl::callWebServer($url, '', 'GET');

    $accessToken['WECHAT_APPSECRET'] = '952524d16b184f93beef7ead9366a549';
    var_dump($accessToken);

    }

    _getAccessToken();

    curl.lib.php

    <?php

    class Curl {
    private static $_ch;
    private static $_header;
    private static $_body;

    private static $_cookie = array();
    private static $_options = array();
    private static $_url = array ();
    private static $_referer = array ();

    /**
    * 调用外部url
    * @param $queryUrl
    * @param $param 参数
    * @param string $method
    * @return bool|mixed
    */
    public static function callWebServer($queryUrl, $param='', $method='get', $is_json=true, $is_urlcode=true) {
    if (empty($queryUrl)) {
    return false;
    }
    $method = strtolower($method);
    $ret = '';
    $param = empty($param) ? array() : $param;
    self::_init();
    if ($method == 'get') {
    $ret = self::_httpGet($queryUrl, $param);
    } elseif($method == 'post') {
    $ret = self::_httpPost($queryUrl, $param, $is_urlcode);
    }
    if(!empty($ret)){
    if($is_json){
    return json_decode($ret, true);
    }else{
    return $ret;
    }
    }
    return true;
    }

    private static function _init() {
    self::$_ch = curl_init();

    curl_setopt(self::$_ch, CURLOPT_HEADER, true);
    curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt(self::$_ch, CURLOPT_FRESH_CONNECT, true);
    }

    public static function setOption($optArray=array()) {
    foreach($optArray as $opt) {
    curl_setopt(self::$_ch, $opt['key'], $opt['value']);
    }
    }

    private static function _close() {
    if (is_resource(self::$_ch)) {
    curl_close(self::$_ch);
    }

    return true;
    }

    private static function _httpGet($url, $query=array()) {

    if (!empty($query)) {
    $url .= (strpos($url, '?') === false) ? '?' : '&';
    $url .= is_array($query) ? http_build_query($query) : $query;
    }

    curl_setopt(self::$_ch, CURLOPT_URL, $url);
    curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
    curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);

    $ret = self::_execute();
    self::_close();
    return $ret;
    }

    private static function _httpPost($url, $query=array(), $is_urlcode=true) {
    if (is_array($query)) {
    foreach ($query as $key => $val) {
    if($is_urlcode){
    $encode_key = urlencode($key);
    }else{
    $encode_key = $key;
    }
    if ($encode_key != $key) {
    unset($query[$key]);
    }
    if($is_urlcode){
    $query[$encode_key] = urlencode($val);
    }else{
    $query[$encode_key] = $val;
    }

    }
    }
    curl_setopt(self::$_ch, CURLOPT_URL, $url);
    curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
    curl_setopt(self::$_ch, CURLOPT_POST, true );
    curl_setopt(self::$_ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);


    $ret = self::_execute();
    self::_close();
    return $ret;
    }

    private static function _put($url, $query = array()) {
    curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'PUT');

    return self::_httpPost($url, $query);
    }

    private static function _delete($url, $query = array()) {
    curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

    return self::_httpPost($url, $query);
    }

    private static function _head($url, $query = array()) {
    curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'HEAD');

    return self::_httpPost($url, $query);
    }

    private static function _execute() {
    $response = curl_exec(self::$_ch);
    $errno = curl_errno(self::$_ch);

    if ($errno > 0) {
    throw new Exception(curl_error(self::$_ch), $errno);
    }
    return $response;
    }
    }

    ?>

    02====================单个编写模拟get提交

    <?php
    define("WECHAT_APPID", 'xxxxxx');
    define("WECHAT_APPSECRET", 'xxxxxxxxxxxxxxxxxxxxx');

    //把填写入库的的appid.定义为常量.给url做参数使用

    //定义模拟get请求

    //开发者工具--文档--进入-->( 开始开发--> 获取接口凭据)--http的请求方式-->get

    function http_get($url){

    $curl = curl_init(); //启动一个curl回话
    curl_setopt($curl,CURLOPT_URL,$url );//,设置当前curl对象的属性,要访问的地址

    curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);//模拟用户使用的浏览器
    curl_setopt($curl,CURLOPT_AUTOREFERER,1);//自动设置referer
    curl_setopt($curl,CURLOPT_TIMEOUT,30);//设置超时限制防止死循环
    curl_setopt($curl,CURLOPT_HEADER,0);//显示返回的header区域内容
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件的形式返回

    $tmpinfo = curl_exec($curl);


    if(curl_errno($curl)){
    return curl_error($curl);
    return FALSE;
    }

    curl_close($curl);

    return $tmpinfo;

    }

    $_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.WECHAT_APPID.'&secret='.WECHAT_APPSECRET;

    $access_token = http_get($_url);

    var_dump($access_token);

    01在服务器上--模拟get请求---运行php文件-->向微信发送get请求-->获取微信返回的access_token.时.-->传递appid,appsecret--设置在公众号后台的-->已近存入微信数据库

    02微信接收到--从服务器端发送的get请求-->检查库中是否存在给appid和密码--存在就随机生成一个加密 access_token

    03 服务器接收到返回access_token-->缓存如文件中(不用不停的请求access_token)--失效后再请求就可以

    04 获得access_token后---就可以使用微信接口了--请求微信接口时.带上access_token--微信检查access_token有效--运行请求

    模拟post提交 数据

    <?php

    function imitate_post($url,$data){

    $ch = curl_init ();
    // print_r($ch); //资源类型

    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, 1 );//正规的HTTP POST
    curl_setopt ( $ch, CURLOPT_HEADER, 0 );//设置header头,是否包含header头,(一般不用)
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); //returntransfer 返回转换,要求结果为json字符串(或者false)
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

    $return = curl_exec( $ch );
    curl_close ( $ch );


    //print_r($return);

    return $return;

    }

    ////////////////////////////////

    $url = "http://www.cms.com/post.php";//这里换成你服务器的地址

    $info = array (
    'name' => 'hao' ,
    'password' => '123'
    );

    $json = imitate_post($url ,$info);

    //返回的json数据 {"access_token":"asdfaxcd132","expire":7200}
    //转换成数组

    $arr = json_decode($json,true);
    //var_dump($arr);
    echo 1;

    //模拟post提交数据给微信
    //就是curl 1初始化,2设置选项,3执行,4关闭.

    ===============本地接收测试

    <?php
    function mylog111($str){
    file_put_contents('./mylog.txt',$str); 
    }

    $data = json_encode($_POST); //获取全部post数据,转化成json,写入txt文件中,很多内容.content加上s

    //$data = file_get_contents("php://input");
    mylog111($data);
    $arr = array('access_token'=>'asdfaxcd132','expire'=>7200);
    echo json_encode($arr);

    ======================模拟get提交.就是 file_get_contents

    <?php

    function imitate_get($url){

    $data = file_get_contents($url);
    var_dump($data);

    }

    $url = 'http://www.cms.com/get.php';

    imitate_get($url);

    ==========get.php

    <html>

    <body>
    hi, iam the superman;
    </body>
    </html>

  • 相关阅读:
    KVM使用入门
    虚拟化技术之KVM
    MySQL初始化以及客户端工具的使用
    Python基础数据类型-字典(dict)
    Golang异常处理-panic与recover
    HTML&CSS基础-overflow
    MySQL数据类型以及基本使用详解
    HTML&CSS基础-display和visibility
    golang数据传输格式-序列化与反序列化
    Golang的文件处理方式-常见的读写姿势
  • 原文地址:https://www.cnblogs.com/bj-tony/p/5278247.html
Copyright © 2011-2022 走看看