Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加微信SDK #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
473 changes: 473 additions & 0 deletions Extend/Vendor/LaneWeChat/README.md

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions Extend/Vendor/LaneWeChat/autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace LaneWeChat;
/**
*
* 自动载入函数
* Created by Lane.
* User: lane
* Date: 14-10-15
* Time: 下午6:13
* E-mail: [email protected]
* WebSite: http://www.lanecn.com
*/
class Autoloader{
const NAMESPACE_PREFIX = 'LaneWeChat\\';
/**
* 向PHP注册在自动载入函数
*/
public static function register(){
spl_autoload_register(array(new self, 'autoload'));
}

/**
* 根据类名载入所在文件
*/
public static function autoload($className){
$namespacePrefixStrlen = strlen(self::NAMESPACE_PREFIX);
if(strncmp(self::NAMESPACE_PREFIX, $className, $namespacePrefixStrlen) === 0){
$className = strtolower($className);
$filePath = str_replace('\\', DIRECTORY_SEPARATOR, substr($className, $namespacePrefixStrlen));
$filePath = realpath(__DIR__ . (empty($filePath) ? '' : DIRECTORY_SEPARATOR) . $filePath . '.lib.php');
if(file_exists($filePath)){
require_once $filePath;
}
}
}
}
51 changes: 51 additions & 0 deletions Extend/Vendor/LaneWeChat/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace LaneWeChat;
/**
* 系统主配置文件.
* @Created by Lane.
* @Author: lane
* @Mail [email protected]
* @Date: 14-8-1
* @Time: 下午1:00
* @Blog: Http://www.lanecn.com
*/
//版本号
define('LANEWECHAT_VERSION', '1.2.2');
define('LANEWECHAT_VERSION_DATE', '2014-10-14');

//微信公众平台相关
define('WECHAT_TOKEN', 'weixin');
define("WECHAT_APPID", 'wxe864803f7865d939');
define("WECHAT_APPSECRET", '0c451f7c33881c22f067dc73847f0b67');
define("WECHAT_URL", 'http://www.lanecn.com');

////-----引入系统所需类库-------------------
////引入错误消息类
//include_once 'core/msg.lib.php';
////引入错误码类
//include_once 'core/msgconstant.lib.php';
////引入CURL类
//include_once 'core/curl.lib.php';
//
////-----------引入微信所需的基本类库----------------
////引入微信处理中心类
//include_once 'core/wechat.lib.php';
////引入微信请求处理类
//include_once 'core/wechatrequest.lib.php';
////引入微信被动响应处理类
//include_once 'core/responsepassive.lib.php';
////引入微信access_token类
//include 'core/accesstoken.lib.php';
//
////-----如果是认证服务号,需要引入以下类--------------
////引入微信权限管理类
//include_once 'core/wechatoauth.lib.php';
////引入微信用户/用户组管理类
//include_once 'core/usermanage.lib.php';
////引入微信主动相应处理类
//include_once 'core/responseinitiative.lib.php';
////引入多媒体管理类
//include_once 'core/media.lib.php';
////引入自定义菜单类
//include_once 'core/menu.lib.php';
?>
2 changes: 2 additions & 0 deletions Extend/Vendor/LaneWeChat/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: 9173weixin
version: 2
67 changes: 67 additions & 0 deletions Extend/Vendor/LaneWeChat/core/accesstoken.lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
namespace LaneWeChat\Core;
/**
* 微信Access_Token的获取与过期检查
* Created by Lane.
* User: lane
* Date: 13-12-29
* Time: 下午5:54
* Mail: [email protected]
* Website: http://www.lanecn.com
*/
class AccessToken{
public static function test(){
echo 'hello world';
}
/**
* 获取微信Access_Token
*/
public static function getAccessToken(){
//检测本地是否已经拥有access_token,并且检测access_token是否过期
$accessToken = self::_checkAccessToken();
if($accessToken === false){
$accessToken = self::_getAccessToken();
}
return $accessToken['access_token'];
}

/**
* @descrpition 从微信服务器获取微信ACCESS_TOKEN
* @return Ambigous|bool
*/
private 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');
if(!isset($accessToken['access_token'])){
return Msg::returnErrMsg(MsgConstant::ERROR_GET_ACCESS_TOKEN, '获取ACCESS_TOKEN失败');
}
$accessToken['time'] = time();
$accessTokenJson = json_encode($accessToken);
//存入数据库
/**
* 这里通常我会把access_token存起来,然后用的时候读取,判断是否过期,如果过期就重新调用此方法获取,存取操作请自行完成
*
* 请将变量$accessTokenJson给存起来,这个变量是一个字符串
*/

return $accessToken;
}

/**
* @descrpition 检测微信ACCESS_TOKEN是否过期
* -10是预留的网络延迟时间
* @return bool
*/
private static function _checkAccessToken(){
//获取access_token。是上面的获取方法获取到后存起来的。
$accessToken = YourDatabase::get('access_token');
if(!empty($accessToken['value'])){
$accessToken = json_decode($accessToken['value'], true);
if(time() - $accessToken['time'] < $accessToken['expires_in']-10){
return $accessToken;
}
}
return false;
}
}
?>
159 changes: 159 additions & 0 deletions Extend/Vendor/LaneWeChat/core/curl.lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
namespace LaneWeChat\Core;
/**
*
* CURL工具
*
* Class Curl
* Created by Lane.
* @Author: lane
* @Mail: [email protected]
* @Date: 14-1-10
* @Time: 下午4:22
* Mail: [email protected]
* Website: http://www.lanecn.com
*/
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);

$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);


$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;
}
}

?>
55 changes: 55 additions & 0 deletions Extend/Vendor/LaneWeChat/core/media.lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace LaneWeChat\Core;
/**
* 多媒体的上传与下载
* Created by Lane.
* User: lane
* Date: 14-8-11
* Time: 上午9:51
* E-mail: [email protected]
* WebSite: http://www.lanecn.com
*/
class Media{
/**
* 多媒体上传。上传图片、语音、视频等文件到微信服务器,上传后服务器会返回对应的media_id,公众号此后可根据该media_id来获取多媒体。
* 上传的多媒体文件有格式和大小限制,如下:
* 图片(image): 1M,支持JPG格式
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
* 视频(video):10MB,支持MP4格式
* 缩略图(thumb):64KB,支持JPG格式
* 媒体文件在后台保存时间为3天,即3天后media_id失效。
*
* @param $filename,文件绝对路径
* @param $type, 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
* @return {"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
*/
public static function upload($filename, $type){
//获取ACCESS_TOKEN
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token='.$accessToken.'&type='.$type;
$data = array();
$data['media'] = $filename;
return Curl::callWebServer($queryUrl, $data, 'POST');
}

/**
* 下载多媒体文件
* @param $mediaId 多媒体ID
* @return 头信息如下
*
* HTTP/1.1 200 OK
* Connection: close
* Content-Type: image/jpeg
* Content-disposition: attachment; filename="MEDIA_ID.jpg"
* Date: Sun, 06 Jan 2013 10:20:18 GMT
* Cache-Control: no-cache, must-revalidate
* Content-Length: 339721
* curl -G "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID"
*/
public static function download($mediaId){
//获取ACCESS_TOKEN
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token='.$accessToken.'&media_id='.$mediaId;
return Curl::callWebServer($queryUrl, '', 'GET');
}
}
Loading