1、已知bug修复

2、微信接口更新
This commit is contained in:
2017-08-26 17:57:10 +08:00
parent cfcd6250ff
commit 356cccd1d4
25 changed files with 1353 additions and 695 deletions

View File

@@ -1,7 +1,20 @@
<?php
// +----------------------------------------------------------------------
// | wechat-php-sdk
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
namespace Wechat;
use Wechat\Lib\Cache;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
@@ -11,7 +24,8 @@ use Wechat\Lib\Tools;
* @author Anyon <zoujingli@qq.com>
* @date 2016/10/26 14:47
*/
class WechatMedia extends Common {
class WechatMedia extends Common
{
const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
const MEDIA_UPLOAD_URL = '/media/upload?';
@@ -36,12 +50,17 @@ class WechatMedia extends Common {
* @param string $type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @return bool|array
*/
public function uploadMedia($data, $type) {
public function uploadMedia($data, $type)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_UPLOAD_URL . "access_token={$this->access_token}" . '&type=' . $type, $data, true);
list($cache_file, $media_content) = ['', base64_decode($data['media'])];
if (!empty($media_content) && ($cache_file = Cache::file($media_content))) {
$data['media'] = "@{$cache_file}";
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_UPLOAD_URL . "access_token={$this->access_token}&type={$type}", $data);
!empty($cache_file) && @unlink($cache_file);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
@@ -60,7 +79,8 @@ class WechatMedia extends Common {
* @param bool $is_video 是否为视频文件,默认为否
* @return bool|array
*/
public function getMedia($media_id, $is_video = false) {
public function getMedia($media_id, $is_video = false)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
@@ -88,7 +108,8 @@ class WechatMedia extends Common {
* @param bool $is_video 是否为视频文件,默认为否
* @return bool|array
*/
public function getMediaWithHttpInfo($media_id, $is_video = false) {
public function getMediaWithHttpInfo($media_id, $is_video = false)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
@@ -97,9 +118,9 @@ class WechatMedia extends Common {
$url_prefix = $is_video ? str_replace('https', 'http', self::API_URL_PREFIX) : self::API_URL_PREFIX;
$url = $url_prefix . self::MEDIA_GET_URL . "access_token={$this->access_token}" . '&media_id=' . $media_id;
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (stripos($url, "https://") !== false) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
@@ -136,12 +157,17 @@ class WechatMedia extends Common {
* @param array $data {"media":'@Path\filename.jpg'}
* @return bool|array
*/
public function uploadImg($data) {
public function uploadImg($data)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
/* 原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀 */
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_UPLOADIMG_URL . "access_token={$this->access_token}", $data, true);
list($cache_file, $media_content) = ['', base64_decode($data['media'])];
if (!empty($media_content) && ($cache_file = Cache::file($media_content))) {
$data['media'] = "@{$cache_file}";
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_UPLOADIMG_URL . "access_token={$this->access_token}", $data);
!empty($cache_file) && @unlink($cache_file);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
@@ -159,20 +185,24 @@ class WechatMedia extends Common {
* 新增的永久素材也可以在公众平台官网素材管理模块中看到
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* @param array $data {"media":'@Path\filename.jpg'}
* @param array $data {"media":'@Path\filename.jpg'}, 支持base64格式
* @param string $type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @param bool $is_video 是否为视频文件,默认为否
* @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'=>'视频标题','introduction'=>'描述')
* @return bool|array
*/
public function uploadForeverMedia($data, $type, $is_video = false, $video_info = array()) {
public function uploadForeverMedia($data, $type, $is_video = false, $video_info = array())
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if ($is_video) {
$data['description'] = Tools::json_encode($video_info);
$is_video && ($data['description'] = Tools::json_encode($video_info));
list($cache_file, $media_content) = ['', base64_decode($data['media'])];
if (!empty($media_content) && ($cache_file = Cache::file($media_content))) {
$data['media'] = "@{$cache_file}";
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_UPLOAD_URL . "access_token={$this->access_token}" . '&type=' . $type, $data, true);
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_UPLOAD_URL . "access_token={$this->access_token}&type={$type}", $data);
!empty($cache_file) && @unlink($cache_file);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
@@ -191,7 +221,8 @@ class WechatMedia extends Common {
* @param array $data 消息结构{"articles":[{...}]}
* @return bool|array
*/
public function uploadForeverArticles($data) {
public function uploadForeverArticles($data)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
@@ -216,16 +247,13 @@ class WechatMedia extends Common {
* @param int $index 更新的文章在图文素材的位置第一篇为0仅多图文使用
* @return bool|array
*/
public function updateForeverArticles($media_id, $data, $index = 0) {
public function updateForeverArticles($media_id, $data, $index = 0)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if (!isset($data['media_id'])) {
$data['media_id'] = $media_id;
}
if (!isset($data['index'])) {
$data['index'] = $index;
}
!isset($data['index']) && $data['index'] = $index;
!isset($data['media_id']) && $data['media_id'] = $media_id;
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_NEWS_UPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
@@ -244,16 +272,14 @@ class WechatMedia extends Common {
* 返回图文消息数组或二进制数据失败返回false
* @param string $media_id 媒体文件id
* @param bool $is_video 是否为视频文件,默认为否
* @return bool|array|raw data
* @return bool|array
*/
public function getForeverMedia($media_id, $is_video = false) {
public function getForeverMedia($media_id, $is_video = false)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('media_id' => $media_id);
//#TODO 暂不确定此接口是否需要让视频文件走http协议
//如果要获取的素材是视频文件时不能使用https协议必须更换成http协议
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_GET_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
if (is_string($result)) {
@@ -265,9 +291,8 @@ class WechatMedia extends Common {
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
} else {
return $result;
}
return $result;
}
return $result;
}
@@ -279,7 +304,8 @@ class WechatMedia extends Common {
* @param string $media_id 媒体文件id
* @return bool
*/
public function delForeverMedia($media_id) {
public function delForeverMedia($media_id)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
@@ -310,15 +336,12 @@ class WechatMedia extends Common {
* 'item'=>array() //素材列表数组,内容定义请参考官方文档
* )
*/
public function getForeverList($type, $offset, $count) {
public function getForeverList($type, $offset, $count)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array(
'type' => $type,
'offset' => $offset,
'count' => $count,
);
$data = array('type' => $type, 'offset' => $offset, 'count' => $count,);
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_BATCHGET_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
@@ -343,7 +366,8 @@ class WechatMedia extends Common {
* 'news_count'=>0 //图文总数量
* )
*/
public function getForeverCount() {
public function getForeverCount()
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
@@ -365,7 +389,8 @@ class WechatMedia extends Common {
* @param array $data 消息结构{"articles":[{...}]}
* @return bool|array
*/
public function uploadArticles($data) {
public function uploadArticles($data)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
@@ -397,7 +422,8 @@ class WechatMedia extends Common {
* "created_at":1398848981
* }
*/
public function uploadMpVideo($data) {
public function uploadMpVideo($data)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}