YlyRpcClient.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace addon\printer\data\sdk\yilianyun\protocol;
  3. use addon\printer\data\sdk\yilianyun\config\YlyConfig;
  4. use Exception;
  5. class YlyRpcClient{
  6. private $clientId;
  7. private $clientSecret;
  8. private $requestUrl;
  9. private $token;
  10. private $log;
  11. public function __construct($token, YlyConfig $config)
  12. {
  13. $this->clientId = $config->getClientId();
  14. $this->clientSecret = $config->getClientSecret();
  15. $this->requestUrl = $config->getRequestUrl();
  16. $this->log = $config->getLog();
  17. $this->token = $token;
  18. }
  19. public function call($action, array $params)
  20. {
  21. $time = time();
  22. $params = array_merge(array(
  23. 'client_id' => $this->clientId,
  24. 'timestamp' => $time,
  25. 'sign' => $this->getSign($time),
  26. 'id' => $this->uuid4(),
  27. 'access_token' => $this->token,
  28. ), $params);
  29. $result = $this->send($params, $this->requestUrl . '/' .$action);
  30. $response = json_decode($result, false, 512, JSON_BIGINT_AS_STRING);
  31. return $response;
  32. }
  33. public function getSign($timestamp)
  34. {
  35. return md5(
  36. $this->clientId.
  37. $timestamp.
  38. $this->clientSecret
  39. );
  40. }
  41. public function uuid4(){
  42. mt_srand((double)microtime() * 10000);
  43. $charid = strtolower(md5(uniqid(rand(), true)));
  44. $hyphen = '-';
  45. $uuidV4 =
  46. substr($charid, 0, 8) . $hyphen .
  47. substr($charid, 8, 4) . $hyphen .
  48. substr($charid, 12, 4) . $hyphen .
  49. substr($charid, 16, 4) . $hyphen .
  50. substr($charid, 20, 12);
  51. return $uuidV4;
  52. }
  53. public function send($data,$url)
  54. {
  55. $requestInfo = http_build_query($data);
  56. $log = $this->log;
  57. if ($log != null) {
  58. $log->info("request data: " . $requestInfo);
  59. }
  60. $curl = curl_init(); // 启动一个CURL会话
  61. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  62. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  63. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  64. 'Expect:'
  65. )); // 解决数据包大不能提交
  66. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  67. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  68. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  69. curl_setopt($curl, CURLOPT_POSTFIELDS, $requestInfo); // Post提交的数据包
  70. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
  71. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  72. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  73. $response = curl_exec($curl); // 执行操作
  74. if (curl_errno($curl)) {
  75. if ($log != null) {
  76. $log->error("error: " . curl_error($curl));
  77. }
  78. throw new Exception(curl_error($curl));
  79. }
  80. if ($log != null) {
  81. $log->info("response: " . $response);
  82. }
  83. curl_close($curl); // 关键CURL会话
  84. return $response; // 返回数据
  85. }
  86. }