YunqianRequest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2021/3/1
  6. * Time: 14:23
  7. */
  8. namespace app\common\services;
  9. class YunqianRequest
  10. {
  11. private $pa;
  12. private $reqURL;
  13. private $app_id;
  14. private $app_secret;
  15. public function __construct($pa,$reqURL,$app_id,$app_secret)
  16. {
  17. $this->pa = $pa;
  18. $this->reqURL = $reqURL;
  19. $this->app_id = $app_id;
  20. $this->app_secret = $app_secret;
  21. }
  22. public function getResult()
  23. {
  24. $array = $this->http_post_data($this->pa, $this->generateHeader(),$this->reqURL);
  25. $json_string = $array[1];
  26. $res = json_decode($json_string,true);
  27. return $res;
  28. }
  29. private function generateHeader(){
  30. $millisecond = $this->getMillisecond();//为服务端时间前后5分钟内可以访问
  31. $nonceStr = $this->getNonceStr(16);//10到50位字符串
  32. $header=[
  33. 'appId:'.$this->app_id,
  34. 'signature:'.$this->getSignature($this->app_id,$this->app_secret,$nonceStr,$millisecond),
  35. 'nonceStr:'.$nonceStr,
  36. 'timestamp:'.$millisecond,
  37. 'Content-Type:application/json',
  38. ];
  39. return $header;
  40. }
  41. //计算请求签名值
  42. private function getSignature($appId, $appSecret,$nonceStr,$millisecond)
  43. {
  44. $s = $appId.'_'.$appSecret.'_'.$nonceStr.'_'.$millisecond;
  45. $signature = strtoupper(md5($s));
  46. return $signature;
  47. }
  48. //模拟发送POST请求
  49. /**
  50. * 模拟发送POST 方式请求
  51. * @param $url
  52. * @param $data
  53. * @param $projectId
  54. * @param $signature
  55. * @return array
  56. */
  57. private function http_post_data( $data, $header,$url) {
  58. $ch = curl_init();
  59. curl_setopt($ch, CURLOPT_POST, 1);
  60. curl_setopt($ch, CURLOPT_URL, $url);
  61. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  62. curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
  63. ob_start();
  64. curl_exec($ch);
  65. $return_content = ob_get_contents();
  66. ob_end_clean();
  67. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  68. //echo curl_errno($ch);
  69. return array($return_code, $return_content);
  70. }
  71. public function curl_post($postdata = '', $header = '', $url,$options = array())
  72. {
  73. $ch = curl_init($url);
  74. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  75. curl_setopt($ch, CURLOPT_POST, 1);
  76. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  77. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  78. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  79. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  80. curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
  81. if (!empty($options)) {
  82. curl_setopt_array($ch, $options);
  83. }
  84. $data = curl_exec($ch);
  85. curl_close($ch);
  86. return $data;
  87. }
  88. /**
  89. *
  90. * 产生随机字符串,不长于50位
  91. * @param int $length
  92. * @return 产生的随机字符串
  93. */
  94. private function getNonceStr($length = 50)
  95. {
  96. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  97. $str ="";
  98. for ( $i = 0; $i < $length; $i++ ) {
  99. $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  100. }
  101. return $str;
  102. }
  103. //获取当前时间戳(毫秒级)
  104. private function getMillisecond()
  105. {
  106. list($s1, $s2) = explode(' ', microtime());
  107. return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
  108. }
  109. }