ZopClient.php 2.0 KB

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