微信接口開發1--向微信發送請求--獲取access_token

//隨便放置一個php文件在服務器上.執行該方法--調用模擬get提交---到微信-->得到微信返回的access_tokenphp

不建議本身編寫模擬get提交方法.html

建議直接導入微信框架LaneWeChat 中的curl.lib.php文件數據庫

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

getaccess_tokyn.php文件api

<?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>

相關文章
相關標籤/搜索