PHPackages                             zyblog/wx-mp-cloud-http-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. zyblog/wx-mp-cloud-http-api

ActiveLibrary

zyblog/wx-mp-cloud-http-api
===========================

wechat-miniprogram-cloud-http-api

0.1.6(5y ago)4151[1 PRs](https://github.com/zhangyue0503/wechat-miniprogram-cloud-http-api/pulls)PHP

Since Nov 29Pushed 3y ago1 watchersCompare

[ Source](https://github.com/zhangyue0503/wechat-miniprogram-cloud-http-api)[ Packagist](https://packagist.org/packages/zyblog/wx-mp-cloud-http-api)[ Docs](https://github.com/zhangyue0503/wechat-miniprogram-cloud-http-api)[ RSS](/packages/zyblog-wx-mp-cloud-http-api/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (1)Versions (9)Used By (0)

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

[](#微信小程序云服务http-api封装)

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

云开发文档：

Composer安装：

```
composer require zyblog/wx-mp-cloud-http-api
```

返回值说明
=====

[](#返回值说明)

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

```
Array
(
    [errcode] => 0
    [errmsg] => ok
    [resp_data] => {"sum":3}
    [wmc_request_url] => https://api.weixin.qq.com/tcb/invokecloudfunction
    [wmc_request_params] => Array
        (
            [body_params] => {"a":1,"b":2}
            [query_params] => Array
                (
                    [access_token] =>  27_vUmnwaBNAfsgHCEnjnXj5C41vTIH7tNrZpzLk0rNKHSyPZ75fhYmYKDXlDWBUcmiNhhXSU4YcsfRSMgLBspV8yaKcO2GQ-2nwDIaP-ICKPZ6e_vLzaQWMaTsZw8TldUlPnN254xZp0NgfkDcQXYeAJAWDE
                    [name] => add
                    [env] => test-app
                )

        )

)
```

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

名称说明wmc\_request\_url请求的接口URL地址wmc\_request\_params请求参数 body\_paramsPOST BODY参数 query\_params链接上的Query参数另外，表示参数有问题直接拦截时，只返回errcode（-100000或-100001）和errmsg，如：

```
Array
(
    [errcode] => -100000
    [errmsg] => 方法名不能为空，空参数不能为''空字符串，默认为'{}'！
)
```

使用方式
====

[](#使用方式)

> ### 初始化调用api
>
> [](#初始化调用api)

```
$cloudApi = new CloudApi($env, $accessToken);
```

名称说明$env云开发环境ID$accessToken接口调用凭证这两个参数是必须要有的，在微信开发者工具的云开发管理中获取云开发环境ID，通过小程序的appid和appsecret获得接口调用凭证，见[获取AccessToken](#%E8%8E%B7%E5%8F%96AccessToken)。

> ### 获取AccessToken
>
> [](#获取accesstoken)

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

```
$cloudApi->token()->getWxAccessToken($appId, $appSecret);

// 直接使用静态方法获取
CloudApi::getWxAccessToken($appId, $appSecret);
```

名称说明$appId小程序唯一凭证，即 AppID$appSecret小程序唯一凭证密钥，即 AppSecret参考微信文档：

> ### 触发云函数
>
> [](#触发云函数)

```
$cloudApi->callFunction()->call($name, $postBody = '{}');
```

名称说明$name云函数名称$postBody云函数的传入参数，具体结构由开发者定义，默认为空{}例如我们有一个云函数，就像微信提供的示例一样，做两个数相加操作，如：

```
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  return {
    sum: event.a + event.b
  }
}
```

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

```
$cloudApi->callFunction()->call('add', ["a"=>1, "b"=>2]);
```

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

```
$cloudApi->callFunction()->call('add', '{"a":1, "b":2}');
```

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

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

```
$cloudApi->callFunction()->call('add');
$cloudApi->callFunction()->call('add', '{}');
$cloudApi->callFunction()->call('add', []);
```

微信HTTP API文档：

> ### 获取腾讯云API调用凭证
>
> [](#获取腾讯云api调用凭证)

```
$cloudApi->qToken()->getToken($lifespan = 7200);
```

名称说明$lifespan有效期（单位为秒，最大7200，默认7200）微信HTTP API文档：

> ### 文件操作
>
> [](#文件操作)

#### **上传文件**

[](#上传文件)

```
$cloudApi->store()->upload($path, $file);
```

名称说明$path上传路径，要带文件名，如：test/aaa/a.jpg$file文件二进制流数据，如：file\_get\_contents('a.jpg')文件上传的上传路径中的文件目录需要在微信开发者工具云开发管理中创建，路径错误无法上传。

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

上传路径需要有文件名，例如：

```
$cloudApi->store()->upload('test/aaa/a.jpg', file_get_contents('a.jpg')); // 上传到test/aaa/目录下
$cloudApi->store()->upload('a.jpg', file_get_contents('a.jpg')); // 上传到根目录下
```

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

```
Array
(
    [errcode] => 0
    [errmsg] => ok
    [url] => https://cos.ap-shanghai.myqcloud.com……
    [token] => ……
    [authorization] => ……
    [file_id] => cloud://xxxxxxxxx/ass.txt    // 重要，需要保存
    [cos_file_id] => ……
    [wmc_request_url] => https://api.weixin.qq.com/tcb/uploadfile
    [wmc_request_params] => Array
        (
            [body_params] => {"env":"xxxxx","path":"ass.txt"}
            [query_params] => Array
                (
                    [access_token] => xxxxxx
                )

        )

)
```

微信HTTP API文档：

#### **获取文件下载链接**

[](#获取文件下载链接)

```
$cloudApi->store()->download($fileList);
```

名称说明$fileList文件列表，内部结构格式如下```
$fileList = [
    [
        "fileid"=>'xxxxx',
        "max_age"=>7200
    ],
    ……
];
```

名称说明fileid文件file\_idmax\_age下载链接有效期，最大7200```
$cloudApi->store()->download([
    [
        "fileid"=>"cloud://xxxxxxx/ass.txt",
        "max_age"=>7200
    ]
]);
```

返回值参考微信文档。

微信HTTP API文档：

#### **删除文件**

[](#删除文件)

```
$cloudApi->store()->delete($fileIdList);
```

名称说明$fileIdList文件ID列表，简单数组格式，\['id1', 'id2'\]```
$cloudApi->store()->delete([
    "cloud://xxxxxxxxxx/as.jpg",
]);
```

微信HTTP API文档：

> ### 数据库操作
>
> [](#数据库操作)

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

#### **集合操作**

[](#集合操作)

```
// 获取集合列表
$cloudApi->db()->getConnections($limit, $offset);
```

名称说明$limit集合数量，可选，默认10$offset偏移量，可选微信HTTP API文档：

```
// 添加集合
$cloudApi->collection()->createCollections($name);
```

名称说明$name集合名称，必填微信HTTP API文档：

```
// 删除集合
$cloudApi->collection()->deleteCollections($name);
```

名称说明$name集合名称，必填微信HTTP API文档：

#### **集合添加操作**

[](#集合添加操作)

```
$cloudApi->collection($collectionName)->add($data); // 单条
$cloudApi->collection($collectionName)->addMulite($data); // 多条
```

名称说明$collectionName集合名称$data添加的内容，可以是数组键值对形式内容，也可以是自己准备好的字符串```
$data = [
[
    'title'=>'测试1',
    'sort' => 1,
    'content'=>'测试1的内容',
    'class' => [
        'cid' => 1,
        'name' => '文章',
    ]
],
……
]
```

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

```
$data = '{title:\"测试4\",sort:4,content:\"测试4的内容\",class:{cid:2,name:\"百科\"}}';
```

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

```
$data = '{xxxx},{xxxx}';
```

示例：

```
// 数组形式添加多条数据
$cloudApi->collection('test-2019')->addMulite([
    [
        'title'=>'测试1',
        'sort' => 1,
        'content'=>'测试1的内容',
        'class' => [
            'cid' => 1,
            'name' => '文章',
        ]
    ],
    [
        'title'=>'测试2',
        'sort' => 2,
        'content'=>'测试2的内容',
        'class' => [
            'cid' => 2,
            'name' => '百科',
        ]
    ],
]);

// 数组形式添加单条数据
$cloudApi->collection('test-2019')->add([
    [
        'title'=>'测试3',
        'sort' => 3,
        'content'=>'测试3的内容',
        'class' => [
            'cid' => 2,
            'name' => '百科',
        ]
    ],
]);

// 字符串形式添加数据
$cloudApi->collection('test-2019')->add('{title:\"测试4\",sort:4,content:\"测试4的内容\",class:{cid:2,name:\"百科\"}}');
```

#### **集合修改操作**

[](#集合修改操作)

```
$cloudApi->collection($collectionName)->update($data, $where = [], $orWhere = []);
```

名称说明$collectionName集合名称$data添加的内容，可以是数组键值对形式内容，也可以是自己准备好的字符串$wherewhere条件，支持语法与查询相同，参考查询操作$orWhereorWhere条件，支持语法与查询，参考查询操作下例将title=测试3的数据的sort改为4，并把class里面的cid改为1，name改为文章

```
$cloudApi->collection('test-20191119')->update([
    'sort' => 4,
    'class' => [
            'cid' => 1,
            'name' => '文章',
        ]
], [
    'title'=>'测试3',
]);
```

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

**更新操作支持函数操作**

- set

```
$cloudApi->collection('test-20191119')->update([
    'class [set]' => [
            'cid' => 3,
            'name' => '文章2',
        ]
], [
    'title'=>'测试3',
]);
```

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.set.html)

- remove

```
$cloudApi->collection('test-20191119')->update([
    'content [remove]' => 1
], [
    'title'=>'测试3',
]);
```

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

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.remove.html)

- inc

```
$cloudApi->collection('test-20191119')->update([
    'sort [inc]' => 1
], [
    'title'=>'测试3',
]);
```

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

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.inc.html)

- mul

```
$cloudApi->collection('test-20191119')->update([
    'sort [mul]' => 2
], [
    'title'=>'测试3',
]);
```

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

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.mul.html)

- push

```
$cloudApi->collection('test-20191119')->update([
    'tags [push]' => [
        "each" =>[["app"=>"aaa", "ppa"=>2], ["app"=>"bbb", "ppa"=>5]],
        "sort" => ["ppa" => 1],
        "position" => 1,
        "slice" => -2
    ],
], [
    'title'=>'测试3',
]);
```

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

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.push.html)

- pop

```
$cloudApi->collection('test-20191119')->update([
    'tags [pop]' => 1,
], [
    'title'=>'测试3',
]);
```

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.pop.html)

- unshift

```
$cloudApi->collection('test-20191119')->update([
    'tags [unshift]' => [1122,333,444,'fff'],
], [
    'title'=>'测试3',
]);
```

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.unshift.html)

- shift

```
$cloudApi->collection('test-20191119')->update([
    'tags [shift]' => 1,
], [
    'title'=>'测试3',
]);
```

[小程序文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.shift.html)

#### **集合删除操作**

[](#集合删除操作)

```
$cloudApi->collection($collectionName)->delete($where = [], $orWhere = []);
```

名称说明$collectionName集合名称$wherewhere条件，支持语法与查询相同，参考查询操作$orWhereorWhere条件，支持语法与查询，参考查询操作下例将删除title=测试4

```
$cloudApi->collection('test-20191119')->delete([
    'title'=>'测试4',
]);
```

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

#### **集合查询操作**

[](#集合查询操作)

```
$cloudApi->collection($collectionName)->get($where, $orWhere, $limit, $orderBy, $field);
```

名称说明$collectionName集合名称$wherewhere条件，\["key \[...\]" =&gt; (value=array,string,number),……\]$orWhereorWhere条件，\[\["key \[...\]" =&gt; (value=array,string,number)\],……\]，注意orWhere是二维数组$limit分页，\[$limit, $offset\]$orderBy排序，\["key asc$field查询字段名，\["key", ……\]查询操作所有的条件都不是必须的，没有任何条件将返回全部的文档列表，在这些参数中主要是where条件的支持语法比较多，我们先看一个实例：

```
$where = [
    "sort [>]" => 1,
];
$orWhere = [
    ["title" => '测试2'],
    ["title" => '测试3'],
];
$limit = [10, 0];
$orderBy = ['sort asc', 'class.cid desc'];
$field = ["class.name", "content", "sort", "title"];
$cloudApi->collection('test-20191119')->get($where, $orWhere, $limit, $orderBy, $field);

// 组合而成的postBody
// {"env":"acp-xxx","query":"db.collection(\"test-20191119\").where(_.and([{sort:_.gt(10)},_.or([{title:\"测试2\"},{title:\"测试3\"}])])).rBy(\"sort\",\"asc\").orderBy(\"cid\", \"desc\").limit(10).skip(0).field({class:{name:true},content:true,sort:true,title:true}).get()"}
```

上述实例中各项说明为：

- $where：sort 大于 1 的
- $orWhere：title 等于 测试2 或者 测试3 的
- $limit：从0开始的10条数据
- $orderBy：按照 sort 正序并且 class.cid 倒序
- $field：显示指定的 class.name 、 content 、 sort 、 title 字段

**where与orWhere所支持的函数操作**

- \[=\] \[eq\] 等于

```
$where = ['title [=]' => '测试3'];
$where = ['title [eq]' => '测试3'];
$where = ['title' => '测试3'];
```

它们的效果是一致的。

- \[\_eq\] 等于对象

```
$where = [
    "class [_eq]" => [
        "cid" => 2,
        "name" => "百科",
    ],
];
```

- \[gt\] \[&gt;\] 大于

```
$where = ["sort [>]" => 1];
$where = ["sort [gt]" => 1];
```

- \[gte\] \[&gt;=\] 大于等于

```
$where = ["sort [>=]" => 1];
$where = ["sort [gte]" => 1];
```

- \[lt\] \[&lt;\] 小于

```
$where = ["sort [=]" => 2
]
// {class:{cid:_.gte(2)}}
```

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

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

```
$cloudApi->collection('test-20191119')->setQuery('db.collection(\"test-20191119\").get()')->execute(\zyblog\wxMpCloudHttpApi\Config::$DATABASE_QUERY);

// {"env":"acp-4ff2bb","query":"db.collection(\"test-20191119\").get()"}
```

#### **文档查询操作**

[](#文档查询操作)

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

```
$cloudApi->collection($collectionName)->doc($docId)->get($where, $orWhere, $limit, $orderBy, $field);
```

名称说明$collectionName集合名称$docId文档ID$wherewhere条件，\["key \[...\]" =&gt; (value=array,string,number),……\]$orWhereorWhere条件，\[\["key \[...\]" =&gt; (value=array,string,number)\],……\]，注意orWhere是二维数组$limit分页，\[$limit, $offset\]$orderBy排序，\["key asc$field查询字段名，\["key", ……\]```
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->get();
```

#### **文档修改操作**

[](#文档修改操作)

```
$cloudApi->collection($collectionName)->doc($docId)->update($data);
```

名称说明$collectionName集合名称$docId文档ID$data添加的内容，可以是数组键值对形式内容，也可以是自己准备好的字符串```
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->update([
    "content"=>"测试1的内容111",
]);
```

#### **文档替换更新操作**

[](#文档替换更新操作)

```
$cloudApi->collection($collectionName)->doc($docId)->set($data);
```

名称说明$collectionName集合名称$docId文档ID$data添加的内容，可以是数组键值对形式内容，也可以是自己准备好的字符串```
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->set([
    "content"=>"测试1的内容111",
]);
```

#### **文档删除操作**

[](#文档删除操作)

```
$cloudApi->collection($collectionName)->doc($docId)->remove();
```

名称说明$collectionName集合名称$docId文档ID```
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->remove();
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~58 days

Recently: every ~1 days

Total

7

Last Release

2006d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/269920e54e4d829e0bc1a7909e62e231f5000c977bafcd8aefa9f0099976f0a6?d=identicon)[zhangyue](/maintainers/zhangyue)

---

Top Contributors

[![zhangyue0503](https://avatars.githubusercontent.com/u/19261189?v=4)](https://github.com/zhangyue0503 "zhangyue0503 (27 commits)")

---

Tags

kindeditoryii2dwzdwz.jsdwz kindeditor

### Embed Badge

![Health badge](/badges/zyblog-wx-mp-cloud-http-api/health.svg)

```
[![Health](https://phpackages.com/badges/zyblog-wx-mp-cloud-http-api/health.svg)](https://phpackages.com/packages/zyblog-wx-mp-cloud-http-api)
```

###  Alternatives

[neuron-core/neuron-ai

The PHP Agentic Framework.

1.8k245.3k20](/packages/neuron-core-neuron-ai)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3731.2M42](/packages/tencentcloud-tencentcloud-sdk-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
