YopRequest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/11/14
  6. * Time: 10:01
  7. */
  8. namespace app\common\modules\yop\sdk;
  9. class YopRequest
  10. {
  11. public $Config;
  12. public $format = 'json';
  13. public $method;
  14. public $locale = "zh_CN";
  15. public $version = "1.0";
  16. public $ImagePath = '';
  17. public $signAlg ;
  18. /**
  19. * 商户编号,易宝商户可不注册开放应用(获取appKey)也可直接调用API
  20. */
  21. public $customerNo;
  22. public $paramMap = array();
  23. public $ignoreSignParams = array('sign');
  24. /**
  25. * 报文是否加密,如果请求加密,则响应也加密,需做解密处理
  26. */
  27. public $encrypt = false;
  28. /**
  29. * 业务结果是否签名,默认不签名
  30. */
  31. public $signRet = false;
  32. /**
  33. * 连接超时时间
  34. */
  35. public $connectTimeout = 30000;
  36. /**
  37. * 读取返回结果超时
  38. */
  39. public $readTimeout = 60000;
  40. /**
  41. * 临时变量,避免多次判断
  42. */
  43. public $isRest = true;
  44. /**
  45. * 可支持不同请求使用不同的appKey及secretKey
  46. */
  47. public $appKey;
  48. /**
  49. * 可支持不同请求使用不同的appKey及secretKey,secretKey只用于本地签名,不会被提交
  50. */
  51. public $secretKey;
  52. /**
  53. * 可支持不同请求使用不同的appKey及secretKey、serverRoot,secretKey只用于本地签名,不会被提交
  54. */
  55. public $yopPublicKey;
  56. /**
  57. * 可支持不同请求使用不同的appKey及secretKey、serverRoot,secretKey只用于本地签名,不会被提交
  58. */
  59. public $serverRoot;
  60. /**
  61. * 临时变量,请求绝对路径
  62. */
  63. public $absoluteURL;
  64. public function __set($name, $value){
  65. // TODO: Implement __set() method.
  66. $this->$name = $value;
  67. }
  68. public function __get($name){
  69. // TODO: Implement __get() method.
  70. return $this->$name;
  71. }
  72. /**
  73. * 格式
  74. * @param string $format 设置格式:xml 或者 json
  75. */
  76. public function setFormat($format) {
  77. if(!empty($format)){
  78. $this->format = $format;
  79. $this->paramMap[$this->Config->FORMAT] = $this->format;
  80. }
  81. }
  82. public function setSignRet($signRet) {
  83. $signRetStr = $signRet?'true':'false';
  84. $this->signRet = $signRet;
  85. $this->addParam($this->Config->SIGN_RETURN, $signRetStr);
  86. }
  87. public function setEncrypt($encrypt) {
  88. $this->encrypt = $encrypt;
  89. }
  90. public function setSignAlg($signAlg) {
  91. $this->signAlg = $signAlg;
  92. }
  93. public function setLocale($locale) {
  94. $this->locale = $locale;
  95. $this->paramMap[$this->Config->LOCALE] = $this->locale;
  96. }
  97. public function setVersion($version) {
  98. $this->version = $version;
  99. $this->paramMap[$this->Config->VERSION] = $this->version;
  100. }
  101. public function setMethod($method) {
  102. $this->method = $method;
  103. //$this->Config->METHOD = $this->method;
  104. $this->paramMap[$this->Config->METHOD] = $this->method;
  105. }
  106. public function __construct($appKey='', $secretKey,$serverRoot='',$yopPublicKey=null){ //定义构造函数
  107. $this->Config = new YopConfig();
  108. $this->signAlg = $this->Config->ALG_SHA1;
  109. if(!empty($appKey)){
  110. $this->appKey = $appKey;
  111. }
  112. else{
  113. $this->appKey = $this->Config->appKey;
  114. }
  115. if(!empty($secretKey)){
  116. $this->secretKey = $secretKey;
  117. }
  118. else{
  119. $this->secretKey = $this->Config->getSecret();
  120. }
  121. if(!empty($yopPublicKey)){
  122. $this->yopPublicKey = $yopPublicKey;
  123. }
  124. else{
  125. $this->yopPublicKey = $this->Config->getSecret();
  126. }
  127. if(!empty($serverRoot)){
  128. $this->serverRoot = $serverRoot;
  129. }
  130. else{
  131. $this->serverRoot = $this->Config->serverRoot;
  132. }
  133. //初始化数组
  134. $this->paramMap[$this->Config->APP_KEY] = $this->appKey;
  135. //$this->paramMap[$this->Config->FORMAT] = $this->format;
  136. $this->paramMap[$this->Config->VERSION] = $this->version;
  137. $this->paramMap[$this->Config->LOCALE] = $this->locale;
  138. // $this->paramMap[$this->Config->TIMESTAMP] = 123456;
  139. $this->paramMap[$this->Config->TIMESTAMP] = time();
  140. }
  141. public function addParam($key,$values,$ignoreSign =false){
  142. if($ignoreSign){
  143. $addParam = array($key=>$values);
  144. $this->ignoreSignParams = array_merge($this->ignoreSignParams,$addParam);
  145. }
  146. $addParam = array($key=>$values);
  147. $this->paramMap = array_merge($this->paramMap,$addParam);
  148. }
  149. public function removeParam($key){
  150. foreach ($this->paramMap as $k => $v){
  151. if($key == $k){
  152. unset($this->paramMap[$k]);
  153. }
  154. }
  155. }
  156. public function getParam($key){
  157. return $this->paramMap[$key];
  158. }
  159. public function encoding(){
  160. foreach ($this->paramMap as $k=>$v){
  161. $this->paramMap[$k] = urlencode($v);
  162. }
  163. }
  164. /**
  165. * 将参数转换成k=v拼接的形式
  166. *
  167. *
  168. */
  169. public function toQueryString(){
  170. $StrQuery="";
  171. foreach ($this->paramMap as $k=>$v){
  172. $StrQuery .= strlen($StrQuery) == 0 ? "" : "&";
  173. $StrQuery.=$k."=".urlencode($v);
  174. }
  175. return $StrQuery;
  176. }
  177. }