小程序碼
獲取小程序碼
接口 A:適用于需要的碼數(shù)量較少的業(yè)務(wù)場(chǎng)景
API:
$miniProgram->appCode->get(string $path, array $optional = []);
其中 $optional 為以下可選參數(shù):
width int
- 默認(rèn)430
二維碼的寬度auto_color
默認(rèn)false
自動(dòng)配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調(diào)line_color
數(shù)組,auto_color
為false
時(shí)生效,使用rgb
設(shè)置顏色,例如:["r" => 0,"g" => 0,"b" => 0]
。
示例代碼:
<?php
$response = $miniProgram->appCode->get('path/to/page');
// 或者
$response = $miniProgram->appCode->get('path/to/page', [
'width' => 600,
// ...
]);
// 或者指定顏色
$response = $miniProgram->appCode->get('path/to/page', [
'width' => 600,
'line_color' => [
'r' => 105,
'g' => 166,
'b' => 134,
],
]);
// $response 成功時(shí)為 EasySwoole\WeChat\Kernel\Psr\StreamResponse 實(shí)例,失敗時(shí)會(huì)返回 bool 值或拋出異常(可通過捕獲異常的形式獲取失敗原因)
// 保存小程序碼到文件
if ($response instanceof \EasySwoole\WeChat\Kernel\Psr\StreamResponse) {
$filename = $response->save('/path/to/directory');
}
// 或
if ($response instanceof \EasySwoole\WeChat\Kernel\Psr\StreamResponse) {
$filename = $response->saveAs('/path/to/directory', 'appcode.png');
}
接口 B:適用于需要的碼數(shù)量極多,或僅臨時(shí)使用的業(yè)務(wù)場(chǎng)景
API:
$miniProgram->appCode->getUnlimit(string $scene, array $optional = []);
其中 $scene
必填,$optinal
與 get
方法一致,多一個(gè) page
參數(shù)。
示例代碼:
<?php
$response = $miniProgram->appCode->getUnlimit('scene-value', [
'page' => 'path/to/page',
'width' => 600,
]);
// $response 成功時(shí)為 EasySwoole\WeChat\Kernel\Psr\StreamResponse 實(shí)例,失敗時(shí)會(huì)返回 bool 值或拋出異常(可通過捕獲異常的形式獲取失敗原因)
// 保存小程序碼到文件
if ($response instanceof \EasySwoole\WeChat\Kernel\Psr\StreamResponse) {
$filename = $response->save('/path/to/directory');
}
// 或
if ($response instanceof \EasySwoole\WeChat\Kernel\Psr\StreamResponse) {
$filename = $response->saveAs('/path/to/directory', 'appcode.png');
}
獲取小程序二維碼
API:
$miniProgram->appCode->getQrCode(string $path, int $width = null);
其中 $path
必填,其余參數(shù)可留空。
示例代碼:
<?php
$response = $miniProgram->appCode->getQrCode('/path/to/page');
// $response 成功時(shí)為 \EasySwoole\WeChat\Kernel\Psr\StreamResponse 實(shí)例,失敗時(shí)會(huì)返回 bool 值或拋出異常(可通過捕獲異常的形式獲取失敗原因)
// 保存小程序碼到文件
if ($response instanceof \EasySwoole\WeChat\Kernel\Psr\StreamResponse) {
$filename = $response->save('/path/to/directory');
}
// 或
if ($response instanceof \EasySwoole\WeChat\Kernel\Psr\StreamResponse) {
$filename = $response->saveAs('/path/to/directory', 'appcode.png');
}