文件
xiaozhongchuf/tests/test_printer_yilianyun.php
2026-09-23 17:04:10 +08:00

88 行
3.2 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
$config = include __DIR__ . "/../application/extra/printer.php";
$clientId = $config["client_id"];
$clientSecret = $config["client_secret"];
$machineCode = $config["sn"];
echo "========== 1. 获取易联云 AccessToken ==========\n";
$timestamp = time();
$sign = md5($clientId . $timestamp . $clientSecret);
$tokenParams = [
"grant_type" => "client_credentials",
"client_id" => $clientId,
"client_secret" => $clientSecret,
"timestamp" => $timestamp,
"sign" => $sign,
"id" => sprintf("%04d%04d", rand(1, 9999), rand(1, 9999))
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://open-api.yilianyun.net/oauth/oauth");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($tokenParams));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$resTokenStr = curl_exec($ch);
curl_close($ch);
echo "Token 接口返回: " . $resTokenStr . "\n\n";
$resToken = json_decode($resTokenStr, true);
if (isset($resToken["body"]["access_token"])) {
$accessToken = $resToken["body"]["access_token"];
echo "获取 AccessToken 成功: " . $accessToken . "\n\n";
echo "========== 2. 发送易联云打印小票指令 ==========\n";
$content = "<FS2><CB>小众厨房设备商城</CB></FS2>\n";
$content .= "<CB>-- 测试订单 --</CB>\n";
$content .= "--------------------------------\n";
$content .= "订单编号:" . "TEST" . date("YmdHis") . "\n";
$content .= "下单时间:" . date("Y-m-d H:i:s") . "\n";
$content .= "--------------------------------\n";
$content .= "商品名称 数量 单价\n";
$content .= "商用切菜机(测试) 1 ¥69.00\n";
$content .= "隔热垫(测试) 2 ¥15.00\n";
$content .= "--------------------------------\n";
$content .= "合计金额:¥99.00 元\n";
$content .= "客户姓名:测试用户\n";
$content .= "联系电话:13984283762\n";
$content .= "送货地址:贵州省修文县城内配送点\n";
$content .= "--------------------------------\n";
$content .= "<QR>https://chufang.gynhc.com</QR>\n";
$content .= "<CB>谢谢惠顾,欢迎再次光临!</CB>\n";
$printTs = time();
$printSign = md5($clientId . $printTs . $clientSecret);
$printParams = [
"client_id" => $clientId,
"access_token" => $accessToken,
"machine_code" => $machineCode,
"content" => $content,
"origin_id" => "TEST" . date("YmdHis"),
"sign" => $printSign,
"timestamp" => $printTs,
"id" => sprintf("%04d%04d", rand(1, 9999), rand(1, 9999))
];
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://open-api.yilianyun.net/printer/printindex");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($printParams));
curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
$resPrintStr = curl_exec($ch2);
curl_close($ch2);
echo "打印接口返回: " . $resPrintStr . "\n";
} else {
echo "获取 AccessToken 失败,请检查开发者ID与密钥。\n";
}