RpcClient.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. // app/Libraries/CommonHelper.php
  3. namespace App\Libraries;
  4. use Dcat\Admin\Admin;
  5. use JsonRPC\Client;
  6. use Illuminate\Http\Request;
  7. use JsonRPC\HttpClient;
  8. class RpcClient
  9. {
  10. // RPC超时时间
  11. public static $timeout = 5;
  12. /*
  13. * RPC客户端
  14. * @param $procedure string 远程过程调用名称
  15. * @param $params array 远程过程调用参数
  16. */
  17. public static function albumExecute ($procedure, $params = []) {
  18. $apiKey = time();
  19. $apiSecret = env('ALBUM_RPC_SECRET');
  20. $payload = json_encode($params);
  21. $payload = $payload.$apiKey;
  22. $signature = hash_hmac('sha256', $payload, $apiSecret);
  23. $clientHost = env('ALBUM_RPC_URL');
  24. $httpClient = new HttpClient($clientHost);
  25. $httpClient = $httpClient->withTimeout(self::$timeout);
  26. $client = new Client($clientHost,false,$httpClient);
  27. try {
  28. $result = $client->execute( $procedure, $params,[], null,[
  29. 'X-API-Key: '.$apiKey,
  30. 'X-API-Signature: '.$signature,
  31. ]);
  32. return $result;
  33. } catch (\Exception $e) {
  34. dd($e->getMessage());
  35. return ['status'=>false,'msg'=>$e->getMessage(),'data'=>[]];
  36. }
  37. }
  38. }