作者 魏强

添加易联云打印机

正在显示 24 个修改的文件 包含 1156 行增加1 行删除
... ... @@ -29,7 +29,8 @@
"phpoffice/phpexcel": "^1.8",
"ezyang/htmlpurifier": "^4.9",
"weiwei/api-doc": "^1.6",
"hooklife/thinkphp5-wechat": "^1.1"
"hooklife/thinkphp5-wechat": "^1.1",
"yly-openapi/yly-openapi-sdk": "v1.0.1"
},
"extra": {
},
... ...
... ... @@ -28,5 +28,6 @@ return array(
'FontLib\\' => array($vendorDir . '/phenx/php-font-lib/src/FontLib'),
'EasyWeChat\\' => array($vendorDir . '/overtrue/wechat/src'),
'Dompdf\\' => array($vendorDir . '/dompdf/dompdf/src'),
'App\\' => array($vendorDir . '/yly-openapi/yly-openapi-sdk/Lib'),
'Api\\Doc\\' => array($vendorDir . '/weiwei/api-doc/src'),
);
... ...
... ... @@ -83,6 +83,7 @@ class ComposerStaticInit929fb365b9981d29459d58f676689cf7
),
'A' =>
array (
'App\\' => 4,
'Api\\Doc\\' => 8,
),
);
... ... @@ -176,6 +177,10 @@ class ComposerStaticInit929fb365b9981d29459d58f676689cf7
array (
0 => __DIR__ . '/..' . '/dompdf/dompdf/src',
),
'App\\' =>
array (
0 => __DIR__ . '/..' . '/yly-openapi/yly-openapi-sdk/Lib',
),
'Api\\Doc\\' =>
array (
0 => __DIR__ . '/..' . '/weiwei/api-doc/src',
... ...
... ... @@ -1676,5 +1676,49 @@
}
],
"description": "The ThinkPHP5 Api Doc Package"
},
{
"name": "yly-openapi/yly-openapi-sdk",
"version": "1.0.1",
"version_normalized": "1.0.1.0",
"source": {
"type": "git",
"url": "https://github.com/Qzm6826/yilianyun-php-sdk.git",
"reference": "f9a2c780957de6f52fbbedde15e04f3ecac28793"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Qzm6826/yilianyun-php-sdk/zipball/f9a2c780957de6f52fbbedde15e04f3ecac28793",
"reference": "f9a2c780957de6f52fbbedde15e04f3ecac28793",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
},
"time": "2018-08-22T02:37:57+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"App\\": "Lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "曲子陌",
"email": "lwy@yilianyun.net"
}
],
"description": "yly-openapi-php-sdk",
"keywords": [
"yly-openapi-php-sdk"
]
}
]
... ...
*.log
composer.lock
.idea
*.wf
/vendor
\ No newline at end of file
... ...
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm
dist: trusty
sudo: false
install: travis_retry composer install --no-interaction --prefer-source
\ No newline at end of file
... ...
<?php
use App\Oauth\YlyOauthClient;
use App\Api\PrintService;
$client = new YlyOauthClient($config);
$code = ''; //开放型应用商户授权码 获取请看 http://doc2.10ss.net/371769#Code_5
if (empty($code)) {echo 'The authorization code cannot be empty';return;}
try {
$token = $client->getToken($code);
} catch (Exception $e) {
echo $e->getMessage() . "\n";
print_r(json_decode($e->getMessage(), true));
return;
}
$access_token = $token->access_token; //调用API凭证AccessToken
$refresh_token = $token->refresh_token; //刷新AccessToken凭证 失效时间35天
$machine_code = $token->machine_code; //商户授权机器码
$expires_in = $token->expires_in; //AccessToken失效时间30天
$origin_id = ''; //内部订单号(32位以内)
if (empty($machine_code)) {echo 'The machine_code cannot be empty';return;}
if (empty($origin_id)) {echo 'The origin_id cannot be empty';return;}
/**文本接口开始**/
$print = new PrintService($access_token, $config);
//58mm排版 排版指令详情请看 http://doc2.10ss.net/332006
$content = "<FS2><center>**#1 美团**</center></FS2>";
$content .= str_repeat('.', 32);
$content .= "<FS2><center>--在线支付--</center></FS2>";
$content .= "<FS><center>张周兄弟烧烤</center></FS>";
$content .= "订单时间:". date("Y-m-d H:i") . "\n";
$content .= "订单编号:40807050607030\n";
$content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14);
$content .= "<table>";
$content .= "<tr><td>烤土豆(超级辣)</td><td>x3</td><td>5.96</td></tr>";
$content .= "<tr><td>烤豆干(超级辣)</td><td>x2</td><td>3.88</td></tr>";
$content .= "<tr><td>烤鸡翅(超级辣)</td><td>x3</td><td>17.96</td></tr>";
$content .= "<tr><td>烤排骨(香辣)</td><td>x3</td><td>12.44</td></tr>";
$content .= "<tr><td>烤韭菜(超级辣)</td><td>x3</td><td>8.96</td></tr>";
$content .= "</table>";
$content .= str_repeat('.', 32);
$content .= "<QR>这是二维码内容</QR>";
$content .= "小计:¥82\n";
$content .= "折扣:¥4 \n";
$content .= str_repeat('*', 32);
$content .= "订单总价:¥78 \n";
$content .= "<FS2><center>**#1 完**</center></FS2>";
try{
var_dump($print->index($machine_code, $content, $origin_id));
}catch (Exception $e) {
echo $e->getMessage();
}
/**文本接口结束**/
///**图形接口开始**/
//$picturePrint = new PicturePrintService($access_token, $config);
//$content = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497000905083&di=7c3cffef1dd40edffbd0a37c4eabb277&imgtype=0&src=http://img1.touxiang.cn/uploads/20131114/14-054929_462.jpg";
//try{
// var_dump($picturePrint->index($machine_code, $content, $origin_id));
//}catch (Exception $e) {
// echo $e->getMessage();
//}
///**图形接口结束**/
///**面单接口开始**/ //打印机型必须为k5;
//$expressPrint = new ExpressPrintService($access_token, $config);
//$content = array(
// "OrderCode"=> "0126578665784971",
// "ShipperCode"=> "SF", //SF YZPY HTKY YD
// "PayType"=> 1,
// "ExpType"=> 1,
// "Cost"=>6.0,
// "OtherCost"=> 7.0,
// "CustomerName" => '1264546',
// "CustomerPwd" => '4545454',
// "MonthCode" => '',
// "Sender"=> array(
// "Company" => "5645645",
// "Name" => "Taylor",
// "Mobile" => "15018442396",
// "ProvinceName" => "上海",
// "CityName" => "上海",
// "PostCode" => '61000',
// "ExpAreaName" => "青浦区",
// "Address" => "明珠路73号"
// ),
// "Receiver"=> array(
// "Company"=> "789789",
// "Name"=> "Yann",
// "Mobile"=> "15018442396",
// "ProvinceName"=> "北京",
// "CityName"=> "北京",
// "PostCode" => '61000',
// "ExpAreaName"=> "朝阳区",
// "Address"=> "三里屯街道雅秀大厦"
// ),
// "Commodity" => array(
// array(
// "GoodsName"=> "鞋子",
// )
// ),
// "AddService"=> array(
// array(
// "Name"=> "COD",
// "Value"=> "1020",
// "CustomerID" => "44564"
// )
// ),
// "StartDate" => date("y-M-d H:i:s",time() + 7200),
// "Weight"=> 1.0,
// "Quantity"=> 1,
// "Volume"=> 0.0,
// "Remark"=> "小心轻放",
//);
//
//try{
// var_dump($expressPrint->index($machine_code, $content, $origin_id));
//}catch (Exception $e) {
// echo $e->getMessage();
//}
///**面单接口结束**/
\ No newline at end of file
... ...
<?php
use App\Oauth\YlyOauthClient;
use App\Api\PrintService;
use App\Api\PicturePrintService;
use App\Api\ExpressPrintService;
$client = new YlyOauthClient($config);
try {
$token = $client->getToken();
} catch (Exception $e) {
echo $e->getMessage() . "\n";
print_r(json_decode($e->getMessage(), true));
return;
}
$access_token = $token->access_token; //调用API凭证AccessToken 永久有效,请妥善保存.
$refresh_token = $token->refresh_token; //刷新AccessToken凭证 失效时间35天
$expires_in = $token->expires_in; //自有型应用可忽略此回调参数, AccessToken失效时间30天
$machine_code = ''; //机器码
$origin_id = ''; //内部订单号(32位以内)
if (empty($machine_code)) {echo 'The machine_code cannot be empty';return;}
if (empty($origin_id)) {echo 'The origin_id cannot be empty';return;}
/**文本接口开始**/
$print = new PrintService($access_token, $config);
//58mm排版 排版指令详情请看 http://doc2.10ss.net/332006
$content = "<FS2><center>**#1 美团**</center></FS2>";
$content .= str_repeat('.', 32);
$content .= "<FS2><center>--在线支付--</center></FS2>";
$content .= "<FS><center>张周兄弟烧烤</center></FS>";
$content .= "订单时间:". date("Y-m-d H:i") . "\n";
$content .= "订单编号:40807050607030\n";
$content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14);
$content .= "<table>";
$content .= "<tr><td>烤土豆(超级辣)</td><td>x3</td><td>5.96</td></tr>";
$content .= "<tr><td>烤豆干(超级辣)</td><td>x2</td><td>3.88</td></tr>";
$content .= "<tr><td>烤鸡翅(超级辣)</td><td>x3</td><td>17.96</td></tr>";
$content .= "<tr><td>烤排骨(香辣)</td><td>x3</td><td>12.44</td></tr>";
$content .= "<tr><td>烤韭菜(超级辣)</td><td>x3</td><td>8.96</td></tr>";
$content .= "</table>";
$content .= str_repeat('.', 32);
$content .= "<QR>这是二维码内容</QR>";
$content .= "小计:¥82\n";
$content .= "折扣:¥4 \n";
$content .= str_repeat('*', 32);
$content .= "订单总价:¥78 \n";
$content .= "<FS2><center>**#1 完**</center></FS2>";
try{
var_dump($print->index($machine_code, $content, $origin_id));
}catch (Exception $e) {
echo $e->getMessage();
}
/**文本接口结束**/
///**图形接口开始**/
//$picturePrint = new PicturePrintService($access_token, $config);
//$content = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497000905083&di=7c3cffef1dd40edffbd0a37c4eabb277&imgtype=0&src=http://img1.touxiang.cn/uploads/20131114/14-054929_462.jpg";
//try{
// var_dump($picturePrint->index($machine_code, $content, $origin_id));
//}catch (Exception $e) {
// echo $e->getMessage();
//}
///**图形接口结束**/
///**面单接口开始**/ //打印机型必须为k5;
//$expressPrint = new ExpressPrintService($access_token, $config);
//$content = array(
// "OrderCode"=> "0126578665784971",
// "ShipperCode"=> "SF", //SF YZPY HTKY YD
// "PayType"=> 1,
// "ExpType"=> 1,
// "Cost"=>6.0,
// "OtherCost"=> 7.0,
// "CustomerName" => '1264546',
// "CustomerPwd" => '4545454',
// "MonthCode" => '',
// "Sender"=> array(
// "Company" => "5645645",
// "Name" => "Taylor",
// "Mobile" => "15018442396",
// "ProvinceName" => "上海",
// "CityName" => "上海",
// "PostCode" => '61000',
// "ExpAreaName" => "青浦区",
// "Address" => "明珠路73号"
// ),
// "Receiver"=> array(
// "Company"=> "789789",
// "Name"=> "Yann",
// "Mobile"=> "15018442396",
// "ProvinceName"=> "北京",
// "CityName"=> "北京",
// "PostCode" => '61000',
// "ExpAreaName"=> "朝阳区",
// "Address"=> "三里屯街道雅秀大厦"
// ),
// "Commodity" => array(
// array(
// "GoodsName"=> "鞋子",
// )
// ),
// "AddService"=> array(
// array(
// "Name"=> "COD",
// "Value"=> "1020",
// "CustomerID" => "44564"
// )
// ),
// "StartDate" => date("y-M-d H:i:s",time() + 7200),
// "Weight"=> 1.0,
// "Quantity"=> 1,
// "Volume"=> 0.0,
// "Remark"=> "小心轻放",
//);
//
//try{
// var_dump($expressPrint->index($machine_code, $content, $origin_id));
//}catch (Exception $e) {
// echo $e->getMessage();
//}
///**面单接口结束**/
\ No newline at end of file
... ...
<?php
include __DIR__ . '/init.php';
if (GRANTTYPE == 'client_credentials') {
include_once __DIR__ . '/ClientMode/callback.php';
}
if (GRANTTYPE == 'authorization_code') {
include_once __DIR__ . '/AuthorizationCodeMode/callback.php';
}
... ...
<?php
include dirname(__DIR__) . '/Lib/Autoloader.php';
use App\Config\YlyConfig;
$grantType = 'client_credentials'; //'client_credentials' 自有型应用; 'authorization_code' 开放型应用
define('GRANTTYPE', $grantType);
$clientId = ''; //应用id
$clientSecret = ''; //应用密钥
$config = new YlyConfig($clientId, $clientSecret);
... ...
<?php
namespace App\Api;
class ExpressPrintService extends RpcService{
/**
* 面单打印接口
* 不支持机型: k4-wh, k4-wa, m1 (k4系列机型不建议使用不干胶热敏纸)
*
* @param $machineCode string 机器码
* @param $content array 面单数据
* @param $originId string 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母
* @return mixed
*/
public function index($machineCode, $content, $originId)
{
return $this->client->call('expressprint/index', array('machine_code' => $machineCode, 'content' => $content, 'origin_id' => $originId));
}
}
... ...
<?php
namespace App\Api;
class OauthService extends RpcService{
/**
* 设置推送URL接口
*
* @param $cmd string 推送队列键
* @param $url string 推送地址
* @param $status string 推送开关
* @return mixed
*/
public function setPushUrl($cmd, $url, $status = 'open')
{
return $this->client->call('oauth/setpushurl', array('cmd' => $cmd, 'url' => $url, 'status' => $status));
}
}
... ...
<?php
namespace App\Api;
class PicturePrintService extends RpcService{
/**
* 图形打印接口
* 不支持机型: k4-wh, k4-wa, m1
*
* @param $machineCode string 机器码
* @param $pictureUrl string 图片链接地址
* @param $originId string 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母
* @return mixed
*/
public function index($machineCode, $pictureUrl, $originId)
{
return $this->client->call('pictureprint/index', array('machine_code' => $machineCode, 'picture_url' => $pictureUrl, 'origin_id' => $originId));
}
}
\ No newline at end of file
... ...
<?php
namespace App\Api;
class PrintMenuService extends RpcService{
/**
* 添加应用菜单接口
*
* @param $machineCode string 机器码
* @param $content string 菜单详情(json)
* @return mixed
*/
public function addPrintMenu($machineCode, $content)
{
return $this->client->call('printmenu/addprintmenu', array('machine_code' => $machineCode, 'content' => $content));
}
}
\ No newline at end of file
... ...
<?php
namespace App\Api;
class PrintService extends RpcService{
/**
* 打印接口
*
* @param $machineCode string 机器码
* @param $content string 打印内容
* @param $originId string 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母
* @return mixed
*/
public function index($machineCode, $content, $originId)
{
return $this->client->call('print/index', array('machine_code' => $machineCode, 'content' => $content, 'origin_id' => $originId));
}
}
\ No newline at end of file
... ...
<?php
namespace App\Api;
class PrinterService extends RpcService{
/**
* 自有型应用授权终端
*
* @param $machineCode string 机器码
* @param $mSign string 机器密钥
* @param string $printName 打印机昵称
* @param string $phone gprs卡号
* @return mixed
*/
public function addPrinter($machineCode, $mSign, $printName = '', $phone = '')
{
$params = array(
'machine_code' => $machineCode,
'msign' => $mSign,
);
if (!empty($phone)) {
$params['phone'] = $phone;
}
if (!empty($printName)) {
$params['print_name'] = $printName;
}
return $this->client->call('printer/addprinter', $params);
}
/**
* 设置内置语音接口
* 注意: 仅支持K4-WA、K4-GAD、K4-WGEAD型号
*
* @param $machineCode string 机器码
* @param $content string 在线语音地址链接 or 自定义语音内容
* @param bool $isFile true or false
* @param string $aid int 0~9 , 定义需设置的语音编号,若不提交,默认升序
* @return mixed
*/
public function setVoice($machineCode, $content, $isFile = false, $aid = '')
{
$params = array(
'machine_code' => $machineCode,
'content' => $content,
'is_file' => $isFile,
);
if (!empty($aid)){
$params ['aid'] = $aid;
}
return $this->client->call('printer/setvoice', $params);
}
/**
* 删除内置语音接口
* 注意: 仅支持K4-WA、K4-GAD、K4-WGEAD型号
*
* @param $machineCode string 机器码
* @param $aid int 0 ~ 9 编号
* @return mixed
*/
public function deleteVoice($machineCode, $aid)
{
return $this->client->call('printer/deletevoice', array('machine_code' => $machineCode, 'aid' => $aid));
}
/**
* 删除终端授权接口
*
* @param $machineCode string 机器码
* @return mixed
*/
public function deletePrinter($machineCode)
{
return $this->client->call('printer/deleteprinter', array('machine_code' => $machineCode));
}
/**
* 关机重启接口
*
* @param $machineCode string 机器码
* @param $responseType string restart or shutdown
* @return mixed
*/
public function shutdownRestart($machineCode, $responseType)
{
return $this->client->call('printer/shutdownrestart', array('machine_code' => $machineCode, 'response_type' => $responseType));
}
/**
* 声音调节接口
*
* @param $machineCode string 机器码
* @param $voice string 音量 0 or 1 or 2 or 3
* @param $responseType string buzzer (蜂鸣器) or horn (喇叭)
* @return mixed
*/
public function setsound($machineCode, $voice, $responseType)
{
return $this->client->call('printer/setsound', array('machine_code' => $machineCode, 'voice' => $voice, 'response_type' => $responseType));
}
/**
* 获取机型打印宽度接口
*
* @param $machineCode string 机器码
* @return mixed
*/
public function printInfo($machineCode)
{
return $this->client->call('printer/printinfo', array('machine_code' => $machineCode));
}
/**
* 获取机型软硬件版本接口
*
* @param $machineCode string 机器码
* @return mixed
*/
public function getVersion($machineCode)
{
return $this->client->call('printer/getversion', array('machine_code' => $machineCode));
}
/**
* 取消所有未打印订单接口
*
* @param $machineCode string 机器码
* @return mixed
*/
public function cancelAll($machineCode)
{
return $this->client->call('printer/cancelall', array('machine_code' => $machineCode));
}
/**
* 取消单条未打印订单接口
*
* @param $machineCode string 机器码
* @param $orderId string 未打印的易联云ID
* @return mixed
*/
public function cancelOne($machineCode, $orderId)
{
return $this->client->call('printer/cancelone', array('machine_code' => $machineCode, 'order_id' => $orderId));
}
/**
* 设置logo接口
*
* @param $machineCode string 机器码
* @param $imgUrl string logo链接地址
* @return mixed
*/
public function setIcon($machineCode, $imgUrl)
{
return $this->client->call('printer/seticon', array('machine_code' => $machineCode, 'img_url' => $imgUrl));
}
/**
* 取消logo接口
*
* @param $machineCode string 机器码
* @return mixed
*/
public function deleteIcon($machineCode)
{
return $this->client->call('printer/deleteicon', array('machine_code' => $machineCode));
}
/**
* 打印方式接口
*
* @param $machineCode string 机器码
* @param $responseType string btnopen or btnclose
* @return mixed
*/
public function btnPrint($machineCode, $responseType)
{
return $this->client->call('printer/btnprint', array('machine_code' => $machineCode, 'response_type' => $responseType));
}
/**
* 接单拒单设置接口
*
* @param $machineCode string 机器码
* @param $responseType string open or close
* @return mixed
*/
public function getOrder($machineCode, $responseType)
{
return $this->client->call('printer/getorder', array('machine_code' => $machineCode, 'response_type' => $responseType));
}
/**
* 获取订单状态接口
*
* @param $machineCode string 机器码
* @param $orderId string 易联云订单id
* @return mixed
*/
public function getOrderStatus($machineCode, $orderId)
{
return $this->client->call('printer/getorderstatus', array('machine_code' => $machineCode, 'order_id' => $orderId));
}
/**
* 获取订单列表接口
*
* @param $machineCode string 机器码
* @param $pageIndex int 第几页
* @param $pageSize int 查询条数
* @return mixed
*/
public function getOrderPagingList($machineCode, $pageIndex = 1 , $pageSize = 10)
{
return $this->client->call('printer/getorderpaginglist', array('machine_code' => $machineCode, 'page_index' => $pageIndex, 'page_size' => $pageSize));
}
}
\ No newline at end of file
... ...
<?php
namespace App\Api;
use App\Config\YlyConfig;
use App\Protocol\YlyRpcClient;
class RpcService{
protected $client;
public function __construct($token, YlyConfig $config)
{
$this->client = new YlyRpcClient($token, $config);
}
}
\ No newline at end of file
... ...
<?php
namespace App;
class Autoloader{
public static function loadByNamespace($name)
{
$class_path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
if (strpos($name, 'App\\') === 0) {
$class_file = __DIR__ . substr($class_path, strlen('App')) . '.php';
}elseif(empty($class_file) || !is_file($class_file)){
$class_file = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $class_path . ".php";
}
if (is_file($class_file)) {
require_once ($class_file);
if (class_exists($name, false)) {
return true;
}
}
return false;
}
}
spl_autoload_register('\App\Autoloader::loadByNamespace');
... ...
<?php
namespace App\Config;
use InvalidArgumentException;
class YlyConfig{
private $clientId = '';
private $clientSecret = '';
private $requestUrl = "https://open-api.10ss.net";
private $log;
public function __construct($clientId, $clientSecret)
{
if ($clientId == null || $clientId == "") {
throw new InvalidArgumentException("clientId is required");
}
if ($clientSecret == null || $clientSecret == "") {
throw new InvalidArgumentException("clientSecret is required");
}
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
}
public function getClientId()
{
return $this->clientId;
}
public function getClientSecret()
{
return $this->clientSecret;
}
public function getRequestUrl()
{
return $this->requestUrl;
}
public function setRequestUrl($requestUrl)
{
$this->requestUrl = $requestUrl;
}
public function getLog()
{
return $this->log;
}
public function setLog($log)
{
if (!method_exists($log, "info")) {
throw new InvalidArgumentException("logger need have method 'info(\$message)'");
}
if (!method_exists($log, "error")) {
throw new InvalidArgumentException("logger need have method 'error(\$message)'");
}
$this->log = $log;
}
}
... ...
<?php
namespace App\Oauth;
use App\Config\YlyConfig;
use Exception;
class YlyOauthClient{
private $clientId;
private $clientSecret;
private $tokenUrl;
private $log;
public function __construct(YlyConfig $config)
{
$this->clientId = $config->getClientId();
$this->clientSecret = $config->getClientSecret();
$this->tokenUrl = $config->getRequestUrl() . '/oauth/oauth';
$this->log = $config->getLog();
}
public function getToken($code = '')
{
$time = time();
$params = array(
'client_id' => $this->clientId,
'timestamp' => $time,
'sign' => $this->getSign($time),
'id' => $this->uuid4(),
'scope' => 'all'
);
$params['grant_type'] = 'client_credentials';
if (!empty($code)) {
$params['code'] = $code;
$params['grant_type'] = 'authorization_code';
}
return $this->send($params);
}
public function refreshToken($refreshToken)
{
$time = time();
$params = array(
'client_id' => $this->clientId,
'timestamp' => $time,
'sign' => $this->getSign($time),
'id' => $this->uuid4(),
'scope' => 'all',
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
);
return $this->send($params);
}
public function getSign($timestamp)
{
return md5(
$this->clientId.
$timestamp.
$this->clientSecret
);
}
public function uuid4(){
mt_srand((double)microtime() * 10000);
$charid = strtolower(md5(uniqid(rand(), true)));
$hyphen = '-';
$uuidV4 =
substr($charid, 0, 8) . $hyphen .
substr($charid, 8, 4) . $hyphen .
substr($charid, 12, 4) . $hyphen .
substr($charid, 16, 4) . $hyphen .
substr($charid, 20, 12);
return $uuidV4;
}
public function send($data)
{
$requestInfo = http_build_query($data);
$log = $this->log;
if ($log != null) {
$log->info("request data: " . $requestInfo);
}
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $this->tokenUrl); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Expect:'
)); // 解决数据包大不能提交
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestInfo); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$requestResponse = curl_exec($curl); // 执行操作
$response = json_decode($requestResponse);
if (curl_errno($curl)) {
if ($log != null) {
$log->error("error: " . curl_error($curl));
}
throw new Exception(curl_error($curl));
}
if (is_null($response)) {
throw new Exception("illegal response :" . $requestResponse);
}
if ($response->error != 0 && $response->error_description != 'success') {
throw new Exception(json_encode($response));
}
if ($this->log != null) {
$this->log->info("response: " . json_encode($response));
}
curl_close($curl); // 关键CURL会话
return $response->body; // 返回数据
}
}
\ No newline at end of file
... ...
<?php
namespace App\Protocol;
use App\Config\YlyConfig;
use Exception;
class YlyRpcClient{
private $clientId;
private $clientSecret;
private $requestUrl;
private $token;
private $log;
public function __construct($token, YlyConfig $config)
{
$this->clientId = $config->getClientId();
$this->clientSecret = $config->getClientSecret();
$this->requestUrl = $config->getRequestUrl();
$this->log = $config->getLog();
$this->token = $token;
}
public function call($action, array $params)
{
$time = time();
$params = array_merge(array(
'client_id' => $this->clientId,
'timestamp' => $time,
'sign' => $this->getSign($time),
'id' => $this->uuid4(),
'access_token' => $this->token,
), $params);
$result = $this->send($params, $this->requestUrl . '/' .$action);
$response = json_decode($result, false, 512, JSON_BIGINT_AS_STRING);
if (is_null($response)) {
throw new Exception("invalid response.");
}
if (
isset($response->error) &&
$response->error != 0
){
$errorDescription = isset($response->body) ? $response->error_description . $response->body : $response->error_description;
throw new Exception('Call method ' . $action . ' error code is ' . $response->error . ' error message is ' . $errorDescription);
}
return $response;
}
public function getSign($timestamp)
{
return md5(
$this->clientId.
$timestamp.
$this->clientSecret
);
}
public function uuid4(){
mt_srand((double)microtime() * 10000);
$charid = strtolower(md5(uniqid(rand(), true)));
$hyphen = '-';
$uuidV4 =
substr($charid, 0, 8) . $hyphen .
substr($charid, 8, 4) . $hyphen .
substr($charid, 12, 4) . $hyphen .
substr($charid, 16, 4) . $hyphen .
substr($charid, 20, 12);
return $uuidV4;
}
public function send($data,$url)
{
$requestInfo = http_build_query($data);
$log = $this->log;
if ($log != null) {
$log->info("request data: " . $requestInfo);
}
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Expect:'
)); // 解决数据包大不能提交
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestInfo); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$response = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
if ($log != null) {
$log->error("error: " . curl_error($curl));
}
throw new Exception(curl_error($curl));
}
if ($log != null) {
$log->info("response: " . $response);
}
curl_close($curl); // 关键CURL会话
return $response; // 返回数据
}
}
... ...
<h1 align="center">yilianyun-php-sdk</h1>
<p align="center">
<a href="https://travis-ci.org/Qzm6826/yilianyun-php-sdk"><img src="https://travis-ci.org/Qzm6826/yilianyun-php-sdk.svg?branch=master" alt="Build Status"></a>
</p>
# Requirement
```
PHP >= 5.4
```
# Installation
```shell
composer require yly-openapi/yly-openapi-sdk:dev-master
```
若composer失败,请换到国内镜像
```shell
composer config -g repo.packagist composer https://packagist.laravel-china.org
```
# Usage
1. 接口类Lib/Api/*.php,集成了所有的易联云接口
2. 配置类Lib/Config/YlyConfig.php
3. 授权类Lib/Oauth/YlyOauthClient.php,获取调用凭证AccessToken,每日上限次数20次,24小时后更新次数
4. 接口调用类Lib/Protocol/YlyRpcClient.php,包括了md5工具函数,Sign工具函数,uuid函数,可以直接用这个类直接进行接口调用
5. 若觉的好用,大佬们请在<a href= 'https://github.com/Qzm6826/yilianyun-php-sdk'>GitHub</a>上给小曲一个Star,在此小曲先感谢各位大佬了,小曲也会时长更新接口的,为大佬们提供方便!
```php
<?php
//composer下加载方式
include_once __DIR__ . "/vendor/autoload.php";
//gitHub下载加载方式
include_once __DIR__ . "/Lib/Autoloader.php";
//初始化配置
use App\Config\YlyConfig;
$config = new YlyConfig('你的应用id', '你的应用密钥');
//获取token
use App\Oauth\YlyOauthClient;
$client = new YlyOauthClient($config);
$token = $client->getToken(); //若是开放型应用请传授权码code
var_dump($token);
//授权打印机(自有型应用使用,开放型应用请跳过该步骤)
use App\Api\PrinterService;
$printer = new PrinterService($token->access_token, $config);
$data = $printer->addPrinter('你的机器码', '你的机器密钥', '机器昵称也可不填', 'gprs卡号没有可不填');
var_dump($data);
//调取文本打印
use App\Api\PrintService;
$print = new PrintService($token->access_token, $config);
$data = $print->index('你的机器码','打印内容排版可看Demo下的callback.php','你的系统内部id自定义32位以内');
var_dump($data);
//调取图形打印
use App\Api\PicturePrintService;
$print = new PicturePrintService($token->access_token, $config);
$data = $print->index('你的机器码','打印内容排版可看Demo下的callback.php','你的系统内部id自定义32位以内');
var_dump($data);
```
\ No newline at end of file
... ...
{
"name": "yly-openapi/yly-openapi-sdk",
"description": "yly-openapi-php-sdk",
"license": "MIT",
"keywords": [
"yly-openapi-php-sdk"
],
"version": "1.0.1",
"authors": [
{
"name": "曲子陌",
"email": "lwy@yilianyun.net"
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
},
"autoload": {
"psr-4": {"App\\": "Lib/"}
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
\ No newline at end of file
... ...