ZopClient.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\library\zop;
  3. use App\library\zop\ZopHttpUtil;
  4. use Illuminate\Support\Facades\Http;
  5. class ZopClient
  6. {
  7. private $zopProperties;
  8. private $httpClient;
  9. /**
  10. * ZopClient constructor.
  11. * @param $zopProperties
  12. */
  13. public function __construct($zopProperties)
  14. {
  15. $this->zopProperties = $zopProperties;
  16. $this->httpClient = new ZopHttpUtil();
  17. }
  18. public function execute($zopRequest)
  19. {
  20. if($zopRequest->getBody()==null) {
  21. $url = $zopRequest->getUrl();
  22. $params = $zopRequest->getParams();
  23. $fixedParams = array();
  24. foreach ($params as $k => $v) {
  25. if (gettype($v) != "string") {
  26. $fixedParams += [$k => json_encode($v)];
  27. } else {
  28. $fixedParams += [$k => $v];
  29. }
  30. }
  31. $str_to_digest = "";
  32. foreach ($fixedParams as $k => $v) {
  33. $str_to_digest = $str_to_digest .$k ."=" .$v ."&";
  34. }
  35. $str_to_digest = substr($str_to_digest, 0, -1) .$this->zopProperties->getKey();
  36. $data_digest = base64_encode(md5($str_to_digest, TRUE));
  37. $headers = array(
  38. "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
  39. "x-companyid: " . $this->zopProperties->getCompanyid(),
  40. "x-datadigest: " . $data_digest
  41. );
  42. return $this->httpClient->post($url, $headers, http_build_query($fixedParams), 2000);
  43. } else {
  44. $url = $zopRequest->getUrl();
  45. $body = $zopRequest->getBody();
  46. $str_to_digest = $body . $this->zopProperties->getKey();
  47. $data_digest = base64_encode(md5($str_to_digest, TRUE));
  48. return Http::withHeaders([
  49. 'Content-Type' => 'application/json; charset=UTF-8',
  50. 'x-companyid' => $this->zopProperties->getCompanyid(),
  51. 'x-datadigest' => $data_digest,
  52. ])->withBody(json_encode((array)json_decode($body), JSON_UNESCAPED_UNICODE), 'application/json')->post($url)->body();
  53. }
  54. }
  55. }