Skip to content

Dev #6

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

Open
wants to merge 10 commits into
base: dev
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

.DS_Store
.vscode
phpunit.xml
tests
vendor
vendor
73 changes: 5 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [介绍](#介绍)
- [安装](#安装)
- [开发指引](#开发指引)
- [文档](#文档)

## 介绍
Expand All @@ -14,81 +15,17 @@ TCB 提供开发应用所需服务和基础设施。tcb-php-sdk 让你可以在

## 安装

require_once 'tcb-php-sdk/autoload.php'; //使用方式:通过源码包引入 ,相对路径自行调整
php-sdk beta 版使用方式
composer require tencentcloudbase/tcb-php-sdk

### 1. 准备工作
> 国内使用时,可切换为国内镜像: composer config -g repo.packagist composer https://packagist.phpcomposer.com

1.1 安装 cli 工具 npm i -g @cloudbase/[email protected]
## 开发指引

1.2 php-sdk 源码包 地址https://github.com/TencentCloudBase/tcb-php-sdk

### 2. 云函数代码编写

示例代码 zip 包下载https://share.weiyun.com/58dQW4M ,基于代码修改即可(代码包中已引入 php-sdk 文件)

[1]index.php 内容

```js
<?php

function main_handler($event, $context)
{
require_once __DIR__ . DIRECTORY_SEPARATOR . 'tcb-admin-php' . DIRECTORY_SEPARATOR . 'autoload.php';
$tcb = new TencentCloudBase\TCB(array("secretId" => "xxx", "secretKey" => "xxx"));

print($tcb)
// your code

return 'helllo world'}
```

[2]tcb.json 内容

```js
{
"deploys": [
{
"name": "phpTestFunc", // 指定当前云函数的function name
"path": "./",
"type": "function",
"envId": "xxx", // tcb envid
"override": true // 是否覆盖同名函数
}
]
}
```

### 3. 云函数部署

3.1 上传云函数:在当前云函数文件夹根目录下输入 tcb deploy --runtime Php7(未登录需要先 tcb login(请正确输入 secretID,secretKey),cli 工具文档参考https://github.com/TencentCloudBase/cloud-base-cli)

部署成功显示 Depoly serverless function xxx success!

### 4. 云函数调用

4.1 进入小程序开发者工具,同步云函数列表,可以看到列表中有上传的云函数 phpTestFunc,编写云函数调用代码

```js
wx.cloud
.callFunction({
name: "phpTestFunc",
data: {}
})
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
```

### 5. php sdk 使用参考内置 readme 文档
[如何使用 php-sdk 开发 php 云函数](docs/tutorial.md)

## 文档

- [初始化](docs/initialization.md)
- [存储](docs/storage.md)
- [数据库](docs/database.md)
- [云函数](docs/functions.md)
- [如何结合 sdk 编写 php 云函数](docs/tutorial.md)
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "tencentcloudbase/tcb-php-sdk",
"description": "tencent cloud base sdk for php",
"version": "1.0.0",
"version": "1.0.3",
"type": "library",
"homepage": "https://github.com/TencentCloudBase/tcb-php-sdk",
"license": "Apache-2.0",
"scripts": {
"test": "phpunit ./tests -c phpunit.xml --filter "
"test": "phpunit ./tests -c ./tests/phpunit.xml "
},
"authors": [
{
Expand All @@ -18,7 +18,7 @@
],
"require": {
"php": ">=7.0.0",
"guzzlehttp/guzzle": "^6.3"
"guzzlehttp/guzzle": ">=6.3.0"
},
"require-dev": {
"phpunit/phpunit": "^7.4"
Expand Down
1 change: 1 addition & 0 deletions docs/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
| --------- | ------ | ---- | --------------------------------------------------------------------------------------------------- |
| secretId | string | 否 | 腾讯云 API 固定密钥对,在云函数内执行可不填。[前往获取](https://console.cloud.tencent.com/cam/capi) |
| secretKey | string | 否 | 同上 |
| sessionToken | string | 否 | 临时密钥 sesstionToken,使用临时密钥时该字段必填。 |
| env | string | 否 | TCB 环境 ID,不填使用默认环境 |
| proxy | string | 否 | 调用接口时使用的 http 代理 url |
| timeout | double | 否 | 调用接口的超时时间(s),默认为 15,即 15 秒 |
Expand Down
16 changes: 11 additions & 5 deletions docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
## 介绍
TCB提供开发应用所需服务和基础设施。tcb-php-sdk 让你可以在服务端(如腾讯云云函数或CVM等)使用php服务访问TCB的的服务。

需要php7及以上版本
TCB 提供开发应用所需服务和基础设施。tcb-php-sdk 让你可以在服务端(如腾讯云云函数或 CVM 等)使用 php 服务访问 TCB 的的服务

## 使用方式:通过源码包引入
require_once 'tcb-php-sdk/autoload.php'; //相对路径自行调整
需要 php7 及以上版本。

## 安装:composer install

composer require tcb-php-sdk

> 国内使用时,可切换为国内镜像: composer config -g repo.packagist composer https://packagist.phpcomposer.com

```php
use TencentCloudBase\TCB;
require 'vendor/autoload.php';

use TencentCloudBase\TCB as TCB;
$tcb = new TCB([]);
```
90 changes: 67 additions & 23 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

### 准备工作

- 安装 cli 工具 npm i -g @cloudbase/[email protected],cli 工具文档参考 https://github.com/TencentCloudBase/cloud-base-cli
- 下载最新的 php-sdk 源码包,sdk 参考内置 readme 文档, 仓库地址 https://github.com/TencentCloudBase/tcb-php-sdk
- 安装 CLI 工具 CLI 工具[文档参考](https://github.com/TencentCloudBase/cloudbase-framework/blob/master/CLI_GUIDE.md)

### 编写 PHP 云函数
```bash
npm i -g @cloudbase/cli
```

- 登录云开发

```bash
cloudbase login
```

- 进入当前 php 项目根目录下进行初始化

- 示例代码包[下载](https://share.weiyun.com/58dQW4M), 可基于示例代码修改(代码包中有引入 tcb-php-sdk,建议使用时下载最新的源码并进行替换)
- 编辑 index.php
```bash
cloudbase init --without-template
```

- 在 php 项目根目录下创建 functions 文件夹存放云函数,进入 functions 文件夹并新建 phpTest 云函数

![](https://main.qcloudimg.com/raw/fb7d40e8af0842a5abc63e6f29d33c42.png)

- phpTest 云函数中新建并编辑入口文件 index.php 如下

```php
<?php
Expand All @@ -25,42 +41,70 @@ function main_handler($event, $context)
}
```

- 编辑 tcb.json
- phpTest 云函数中编辑 composer.json 如下

```json
{
"deploys": [
{
"name": "phpTestFunc", // 指定当前云函数的function name
"path": "./",
"type": "function",
"envId": "xxx", // tcb envid
"override": true // 是否覆盖同名函数
"require": {
"tencentcloudbase/tcb-php-sdk": "1.0"
}
}
```

- 配置

`cloudbase init` 之后会创建云开发的配置文件 `cloudbaserc.json`,可在配置文件的 plugins 里修改和写入插件配置

```json
{
"envId": "xxx", // 替换为开发者自己的envId
"framework": {
"plugins": {
"function": {
"use": "@cloudbase/framework-plugin-function",
"inputs": {
"functionRootPath": "./functions",
"functions": [
{
"name": "phpTest",
"timeout": 60,
"envVariables": {},
"runtime": "Php7",
"memorySize": 128,
"handler": "index.main_handler"
}
]
}
}
}
]
}
}
```

### 云函数部署

- 上传云函数:在当前云函数文件夹根目录下输入 tcb deploy --runtime Php7(未登录需要先 tcb login(请正确输入 secretID,secretKey),部署成功显示 Depoly serverless function xxx success!
php 项目根目录下一键部署

```bash
cloudbase framework:deploy
```

### 云函数调用

- 进入小程序开发者工具,同步云函数列表,可以看到列表中有上传的云函数 phpTestFunc,编写云函数调用代码
- 进入小程序开发者工具,同步云函数列表,可以看到列表中有上传的云函数 phpTest,编写云函数调用代码

示例代码

```javascript
wx.cloud
.callFunction({
name: "phpTestFunc",
data: {}
name: 'phpTest',
data: {},
})
.then((res) => {
console.log(res)
})
.then(res => {
console.log(res);
.catch((err) => {
console.error(err)
})
.catch(err => {
console.error(err);
});
```
8 changes: 4 additions & 4 deletions src/TCB.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($options = [
}

if (array_key_exists('isHttp', $options)) {
$this->config->isHttp = $options['isHttp']; // -> ???????
$this->config['isHttp'] = $options['isHttp']; // -> ???????
}

if (array_key_exists('env', $options)) {
Expand All @@ -58,12 +58,12 @@ public function __construct($options = [

if (array_key_exists('sessionToken', $options)) {
if (!empty($options['sessionToken'])) {
$this->config->sessionToken = $options['sessionToken'];
$this->config['sessionToken'] = $options['sessionToken'];
} else if ($this->config['secretId'] && $this->config['secretKey']) {
$this->config->sessionToken = null;
$this->config['sessionToken'] = null;
} else {
$envSessionToken = getenv('TENCENTCLOUD_SESSIONTOKEN');
$this->config->sessionToken = $envSessionToken ? $envSessionToken : null;
$this->config['sessionToken'] = $envSessionToken ? $envSessionToken : null;
}
}
}
Expand Down