Download the PHP package zyblog/wx-mp-cloud-http-api without Composer

On this page you can find all versions of the php package zyblog/wx-mp-cloud-http-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package wx-mp-cloud-http-api

微信小程序云服务HTTP API封装

根据微信官方文档,使用PHP对HTTP API相关操作进行的封装。

云开发文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html

Composer安装:

返回值说明

所有接口的返回值都按微信HTTP API的返回值原样返回,并在原始返回值的基础上增加了两个字段,如:

上述返回值的内容为调用云函数测试的返回值,在微信原返回值的基础上增加了:

名称 说明
wmc_request_url 请求的接口URL地址
wmc_request_params 请求参数
     body_params POST BODY参数
     query_params 链接上的Query参数

另外,表示参数有问题直接拦截时,只返回errcode(-100000或-100001)和errmsg,如:

使用方式

 

初始化调用api

名称 说明
$env 云开发环境ID
$accessToken 接口调用凭证

这两个参数是必须要有的,在微信开发者工具的云开发管理中获取云开发环境ID,通过小程序的appid和appsecret获得接口调用凭证,见获取AccessToken。

 

获取AccessToken

很多框架都已经有这个功能,这里就有简单的封装了一下。

名称 说明
$appId 小程序唯一凭证,即 AppID
$appSecret 小程序唯一凭证密钥,即 AppSecret

参考微信文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html

 

触发云函数

名称 说明
$name 云函数名称
$postBody 云函数的传入参数,具体结构由开发者定义,默认为空{}

例如我们有一个云函数,就像微信提供的示例一样,做两个数相加操作,如:

在使用接口时就可以这样调用:

当然,$postBody是支持字段串的,用于复杂的参数格式,如:

需要注意的是,$postBody如果是字符串,必须是json格式,传递数组进来将自动进行json转换。

如果云函数没有参数,需要传递{}。在封装的框架中,$postBody是可选参数。如下面例子给云函数传递空的参数:

微信HTTP API文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/functions/invokeCloudFunction.html

 

获取腾讯云API调用凭证

名称 说明
$lifespan 有效期(单位为秒,最大7200,默认7200)

微信HTTP API文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/utils/getQcloudToken.html

 

文件操作

上传文件

名称 说明
$path 上传路径,要带文件名,如:test/aaa/a.jpg
$file 文件二进制流数据,如:file_get_contents('a.jpg')

文件上传的上传路径中的文件目录需要在微信开发者工具云开发管理中创建,路径错误无法上传。

文件上传根据文档会提交两次请求,第一次请求获得凭证及file_id,第二次请求正式上传文件。在系统或本地存储中,应保存file_id用于后续的文件下载链接获取及删除操作。

上传路径需要有文件名,例如:

上传成功后返回的结果为:

微信HTTP API文档: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html

获取文件下载链接

名称 说明
$fileList 文件列表,内部结构格式如下
名称 说明
fileid 文件file_id
max_age 下载链接有效期,最大7200

返回值参考微信文档。

微信HTTP API文档: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html

删除文件

名称 说明
$fileIdList 文件ID列表,简单数组格式,['id1', 'id2']

微信HTTP API文档: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDeleteFile.html

 

数据库操作

重头戏来了,微信云使用的是类似于MongoDb的文档式数据库,但HTTP API提供的能力并不完全,比如不支持聚合等一些函数,所以相关函数的使用请尝试使用云函数进行开发。

集合操作

名称 说明
$limit 集合数量,可选,默认10
$offset 偏移量,可选

微信HTTP API文档: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionGet.html

名称 说明
$name 集合名称,必填

微信HTTP API文档: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionAdd.html

名称 说明
$name 集合名称,必填

微信HTTP API文档: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionDelete.html

集合添加操作

名称 说明
$collectionName 集合名称
$data 添加的内容,可以是数组键值对形式内容,也可以是自己准备好的字符串

如上代码所示,可以多条同时插入。

也可以是自己组装好的字符串,如果是多条插入,不用加[],如下所示:

示例:

集合修改操作

名称 说明
$collectionName 集合名称
$data 添加的内容,可以是数组键值对形式内容,也可以是自己准备好的字符串
$where where条件,支持语法与查询相同,参考查询操作
$orWhere orWhere条件,支持语法与查询,参考查询操作

下例将title=测试3的数据的sort改为4,并把class里面的cid改为1,name改为文章

如果不给where条件,则集合中所有数据都更新,与MySQL类似

更新操作支持函数操作

小程序文档

值并没有特殊意义,会根据key值判断这个key中是否有remove操作,也就是说这里的值可以给任意内容

小程序文档

值表示增加数,必须数字类型,负数表示为减操作,例如当前sort为4,则加1后为5,或使用-2后为2

小程序文档

与inc类似,但注意数学运算规则,如果给负数或者本身数据为负数,则乘积都为负数

小程序文档

数组操作能力,详情查看文档,支持文档中的参数写法。

小程序文档

小程序文档

小程序文档

小程序文档

集合删除操作

名称 说明
$collectionName 集合名称
$where where条件,支持语法与查询相同,参考查询操作
$orWhere orWhere条件,支持语法与查询,参考查询操作

下例将删除title=测试4

如果不给where条件,则集合中所有数据都删除,与MySQL类似

集合查询操作

名称 说明
$collectionName 集合名称
$where where条件,["key [...]" => (value=array,string,number),……]
$orWhere orWhere条件,[["key [...]" => (value=array,string,number)],……],注意orWhere是二维数组
$limit 分页,[$limit, $offset]
$orderBy 排序,["key asc desc", ……]
$field 查询字段名,["key", ……]

查询操作所有的条件都不是必须的,没有任何条件将返回全部的文档列表,在这些参数中主要是where条件的支持语法比较多,我们先看一个实例:

上述实例中各项说明为:

where与orWhere所支持的函数操作

它们的效果是一致的。

regexp是必须的参数,options默认为im

表示sort大于1并且小于40,注意'[gt]'是没有key值的,所有的where条件都支持这种语法,但需要参考微信文档来进行使用

表示sort大于1或者小于40的

字段要加地理位置索引,要在微信开发者工具云开发管理中添加

地理位置字段的添加修改查询说明

地理位置字段比较灵活,不管添加还是修改查询时,建议直接拼装自己需要的查询语句。具体参考微信文档,这里仅给出示例:

[json] 为特殊标识符,表示该字段下的内容不进行解析,直接转换成json格式数据

查询操作参考上方where条件中的相关操作

关于字段名的点操作

不管是where条件、data字段还是field字段中,我们操作的键名都可以进行点加连接操作:

直接使用自行拼装的字符串

我们的所有数据库操作还有更简单粗暴的方式,就是直接使用您准备好的查询语句,也就是接口中body部分里面query的内容,例如:

文档查询操作

在获取到文档id的前提下,可以直接使用文档id对单条文档数据进行操作,文档id就是建立文档时自动或指定生成的"_id"字段

名称 说明
$collectionName 集合名称
$docId 文档ID
$where where条件,["key [...]" => (value=array,string,number),……]
$orWhere orWhere条件,[["key [...]" => (value=array,string,number)],……],注意orWhere是二维数组
$limit 分页,[$limit, $offset]
$orderBy 排序,["key asc desc", ……]
$field 查询字段名,["key", ……]

文档修改操作

名称 说明
$collectionName 集合名称
$docId 文档ID
$data 添加的内容,可以是数组键值对形式内容,也可以是自己准备好的字符串

文档替换更新操作

名称 说明
$collectionName 集合名称
$docId 文档ID
$data 添加的内容,可以是数组键值对形式内容,也可以是自己准备好的字符串

文档删除操作

名称 说明
$collectionName 集合名称
$docId 文档ID

All versions of wx-mp-cloud-http-api with dependencies

PHP Build Version
Package Version
Requires guzzlehttp/guzzle Version ^7.2.0
ext-json Version *
ext-mbstring Version *
ext-curl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zyblog/wx-mp-cloud-http-api contains the following files

Loading the files please wait ...