PHP code example of zyblog / wx-mp-cloud-http-api
1. Go to this page and download the library: Download zyblog/wx-mp-cloud-http-api library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zyblog / wx-mp-cloud-http-api example snippets
composer
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
)
)
)
Array
(
[errcode] => -100000
[errmsg] => 方法名不能为空,空参数不能为''空字符串,默认为'{}'!
)
$cloudApi = new CloudApi($env, $accessToken);
$cloudApi->token()->getWxAccessToken($appId, $appSecret);
// 直接使用静态方法获取
CloudApi::getWxAccessToken($appId, $appSecret);
$cloudApi->callFunction()->call($name, $postBody = '{}');
$cloudApi->callFunction()->call('add', ["a"=>1, "b"=>2]);
$cloudApi->callFunction()->call('add', '{"a":1, "b":2}');
$cloudApi->callFunction()->call('add');
$cloudApi->callFunction()->call('add', '{}');
$cloudApi->callFunction()->call('add', []);
$cloudApi->qToken()->getToken($lifespan = 7200);
$cloudApi->store()->upload($path, $file);
$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
)
)
)
$cloudApi->store()->download($fileList);
$fileList = [
[
"fileid"=>'xxxxx',
"max_age"=>7200
],
……
];
$cloudApi->store()->download([
[
"fileid"=>"cloud://xxxxxxx/ass.txt",
"max_age"=>7200
]
]);
$cloudApi->store()->delete($fileIdList);
$cloudApi->store()->delete([
"cloud://xxxxxxxxxx/as.jpg",
]);
// 获取集合列表
$cloudApi->db()->getConnections($limit, $offset);
// 添加集合
$cloudApi->collection()->createCollections($name);
// 删除集合
$cloudApi->collection()->deleteCollections($name);
$cloudApi->collection($collectionName)->add($data); // 单条
$cloudApi->collection($collectionName)->addMulite($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 = []);
$cloudApi->collection('test-20191119')->update([
'sort' => 4,
'class' => [
'cid' => 1,
'name' => '文章',
]
], [
'title'=>'测试3',
]);
$cloudApi->collection('test-20191119')->update([
'class [set]' => [
'cid' => 3,
'name' => '文章2',
]
], [
'title'=>'测试3',
]);
$cloudApi->collection('test-20191119')->update([
'content [remove]' => 1
], [
'title'=>'测试3',
]);
$cloudApi->collection('test-20191119')->update([
'sort [inc]' => 1
], [
'title'=>'测试3',
]);
$cloudApi->collection('test-20191119')->update([
'sort [mul]' => 2
], [
'title'=>'测试3',
]);
$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',
]);
$cloudApi->collection('test-20191119')->update([
'tags [pop]' => 1,
], [
'title'=>'测试3',
]);
$cloudApi->collection('test-20191119')->update([
'tags [unshift]' => [1122,333,444,'fff'],
], [
'title'=>'测试3',
]);
$cloudApi->collection('test-20191119')->update([
'tags [shift]' => 1,
], [
'title'=>'测试3',
]);
$cloudApi->collection($collectionName)->delete($where = [], $orWhere = []);
$cloudApi->collection('test-20191119')->delete([
'title'=>'测试4',
]);
$cloudApi->collection($collectionName)->get($where, $orWhere, $limit, $orderBy, $field);
$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 = ['title [=]' => '测试3'];
$where = ['title [eq]' => '测试3'];
$where = ['title' => '测试3'];
$where = [
"class [_eq]" => [
"cid" => 2,
"name" => "百科",
],
];
$where = ["sort [>]" => 1];
$where = ["sort [gt]" => 1];
$where = ["sort [>=]" => 1];
$where = ["sort [gte]" => 1];
$where = ["sort [<]" => 30];
$where = ["sort [lt]" => 30];
$where = ["sort [<=]" => 30];
$where = ["sort [lte]" => 30];
$where = ["title [neq]" => "测试2"];
$where = ["title [!=]" => "测试2"];
$where = ["title [<>]" => "测试2"];
$where = ["title [in]" => ["测试2", "测试3"]];
$where = ["title [not in]" => ["测试2", "测试3"]];
$where = ["title [nin]" => ["测试2", "测试3"]];
$where = ["class.name [like]" => "文"];
$where = ["class.name [not like]" => "文"];
$where = ["tags [exists]" => 1]; // 返回存在tags字段的
$where = ["tags [exists]" => 0]; // 返回不存在tags字段的
$where = ["tags [size]" => 3];
$where = ["tags [size]" => 4];
$where = ["sort [mod]" => "5"]; // 模5为0的
$where = ["sort [mod]" => "3,1"]; // 模3余1的,测试时出错403错误,微信文档没有给出说明
$where = [
"class.name [reg]" => [
'regexp' => "文.*[1-9]",
'options' => 'ims'
],
];
$where = [
"tags [exists]" => 0,
];
$where = [
"sort [and]" => [
' [gt]' => 1,
' [lt]' => 40
],
];
// sort:_.and(_.gt(1),_.lt(40))
$where = [
"sort [or]" => [
' [gt]' => 2,
' [lt]' => 40
],
];
// sort:_.or(_.gt(2),_.lt(40))
$where = [
"tags [all]" => [
333, 444
],
];
$where = [
"tags [size]" => 4,
];
// "place":[{"age":15,"area":50},{"age":5,"area":30}]
$where = [
"place [elemMatch]" => [
'area [lt]' => 100,
'age [lt]' => 20
],
];
// "tags":[333,444,'fff']
$where = [
"tags [elemMatch]" => [333,444,'fff'],
];
$where = [
'location [geoNear]' => [
'geometry' => '113.323809, 23.097732',
'minDistance' => 1000,
'maxDistance'=> 5000,
],
];
// 多边形
$where = [
'location [geoWithin]' => [
'geometry' => 'db.Geo.Polygon([db.Geo.LineString([db.Geo.Point(0, 0),db.Geo.Point(3, 2),db.Geo.Point(2, 3),db.Geo.Point(0, 0)])])',
],
];
// 圆形
$where = [
'location [geoWithin]' => [
'centerSphere' => '[[113, 23],' . (100 / 6378.1) . ']',
],
];
$where = [
'location [geoIntersects]' => [
'geometry' => 'db.Geo.Polygon([db.Geo.LineString([db.Geo.Point(0, 0),db.Geo.Point(3, 2),db.Geo.Point(2, 3),db.Geo.Point(0, 0)])])',
],
];
// 添加一个地理位置字段数据
$cloudApi->collection('test-20191119')->add([
'location' => [
'type' => 'Point',
'coordinates [json]' => [113.222, 23.0002],
],
]);
[
"class.cid" => 2
]
// {class:{cid:2}}
[
"class.cid [>=]" => 2
]
// {class:{cid:_.gte(2)}}
$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()"}
$cloudApi->collection($collectionName)->doc($docId)->get($where, $orWhere, $limit, $orderBy, $field);
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->get();
$cloudApi->collection($collectionName)->doc($docId)->update($data);
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->update([
"content"=>"测试1的内容111",
]);
$cloudApi->collection($collectionName)->doc($docId)->set($data);
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->set([
"content"=>"测试1的内容111",
]);
$cloudApi->collection($collectionName)->doc($docId)->remove();
$cloudApi->collection('test-20191119')->doc('392e3683-77c9-44f5-841d-0011847cec85')->remove();