PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/core/model/service/mdl.tbsend.php

http://phpfor.googlecode.com/
PHP | 174 lines | 126 code | 12 blank | 36 comment | 25 complexity | 867fa9330af6f39d526157e0e4a02ce7 MD5 | raw file
  1. <?php
  2. include_once('shopObject.php');
  3. class mdl_tbsend extends shopObject {
  4. var $error;
  5. function setparams($params,$charset='utf-8',$sign_type='MD5'){
  6. if($params['service']==""){
  7. return '??????????';
  8. }
  9. $params['_input_charset']=$charset;
  10. $PrivateKey = $params['PrivateKey'];
  11. unset($params['PrivateKey']);
  12. $params['transport_type'] = 'EXPRESS';
  13. $params['create_transport_type'] = 'EXPRESS';
  14. $params['seller_ip'] = $_SERVER['REMOTE_ADDR'];
  15. //??????????????????
  16. $params = $this->paraFilter($params);
  17. //??????????
  18. $params = $this->argSort($params);
  19. $params['sign'] = $this->buildMysign($params,$PrivateKey,$sign_type = "MD5");
  20. //$params['sign'] = md5(urldecode(http_build_query($params)).$PrivateKey);
  21. $params['sign_type']=strtoupper(trim($sign_type));
  22. return $params;
  23. }
  24. function getUrl($params){
  25. $data = $this->setparams($params);
  26. if(!is_array($data)){
  27. return $data;
  28. }
  29. $url = ALIPAY_GATEWAY.$this->createLinkstring($data);
  30. //$url = ALIPAY_GATEWAY.urldecode(http_build_query($data));
  31. return $url;
  32. }
  33. function getContents($params){
  34. $url = $this->getUrl($params);
  35. if(!$url)
  36. return false;
  37. //$getdata = file_get_contents($url);
  38. $xml_data = $this->getHttpResponse($url,trim(strtolower('utf-8')));
  39. if($xml_data){
  40. $xml=$this->system->loadModel('utility/xml');
  41. $result=$xml->xml2array($xml_data);
  42. if(is_array($result)){
  43. return $result;
  44. }else{
  45. $this->error=$result;
  46. return false;
  47. }
  48. }else{
  49. return false;
  50. }
  51. }
  52. function buildMysign($sort_para,$key,$sign_type = "MD5") {
  53. //??????????“??=???”????“&”????????
  54. $prestr = $this->createLinkstring($sort_para);
  55. //?????????????????????
  56. $prestr = $prestr.$key;
  57. //????????????????
  58. $mysgin = $this->sign($prestr,$sign_type);
  59. return $mysgin;
  60. }
  61. /**
  62. * ??????????“??=???”????“&”????????
  63. * @param $para ???????
  64. * return ??????????
  65. */
  66. function createLinkstring($para) {
  67. $arg = "";
  68. while (list ($key, $val) = each ($para)) {
  69. $arg.=$key."=".$val."&";
  70. }
  71. //??????&??
  72. $arg = substr($arg,0,count($arg)-2);
  73. //???????????????
  74. if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
  75. return $arg;
  76. }
  77. /**
  78. * ?????????????
  79. * @param $para ?????
  80. * return ?????????????????
  81. */
  82. function paraFilter($para) {
  83. $para_filter = array();
  84. while (list ($key, $val) = each ($para)) {
  85. if($key == "sign" || $key == "sign_type" || $val == "")continue;
  86. else $para_filter[$key] = $para[$key];
  87. }
  88. return $para_filter;
  89. }
  90. /**
  91. * ?????
  92. * @param $para ??????
  93. * return ??????
  94. */
  95. function argSort($para) {
  96. ksort($para);
  97. reset($para);
  98. return $para;
  99. }
  100. /**
  101. * ?????
  102. * @param $prestr ????????
  103. * @param $sign_type ???? ????MD5
  104. * return ????
  105. */
  106. function sign($prestr,$sign_type='MD5') {
  107. $sign='';
  108. if($sign_type == 'MD5') {
  109. $sign = md5($prestr);
  110. }
  111. return $sign;
  112. }
  113. /**
  114. * ?????????????????????????????
  115. * ??????????fopen??
  116. * @param $word ??????????? ??????
  117. */
  118. function logResult($word='') {
  119. $fp = fopen("log.txt","a");
  120. flock($fp, LOCK_EX) ;
  121. fwrite($fp,"?????".strftime("%Y%m%d%H%M%S",time())."\n".$word."\n");
  122. flock($fp, LOCK_UN);
  123. fclose($fp);
  124. }
  125. function getHttpResponse($url, $input_charset = '', $time_out = "60") {
  126. $urlarr = parse_url($url);
  127. $errno = "";
  128. $errstr = "";
  129. $transports = "";
  130. $responseText = "";
  131. if($urlarr["scheme"] == "https") {
  132. $transports = "ssl://";
  133. $urlarr["port"] = "443";
  134. } else {
  135. $transports = "tcp://";
  136. $urlarr["port"] = "80";
  137. }
  138. $fp=@fsockopen($transports . $urlarr['host'],$urlarr['port'],$errno,$errstr,$time_out);
  139. if(!$fp) {
  140. die("ERROR: $errno - $errstr<br />\n");
  141. } else {
  142. if (trim($input_charset) == '') {
  143. fputs($fp, "POST ".$urlarr["path"]." HTTP/1.1\r\n");
  144. }
  145. else {
  146. fputs($fp, "POST ".$urlarr["path"].'?_input_charset='.$input_charset." HTTP/1.1\r\n");
  147. }
  148. fputs($fp, "Host: ".$urlarr["host"]."\r\n");
  149. fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  150. fputs($fp, "Content-length: ".strlen($urlarr["query"])."\r\n");
  151. fputs($fp, "Connection: close\r\n\r\n");
  152. fputs($fp, $urlarr["query"] . "\r\n\r\n");
  153. while(!feof($fp)) {
  154. $responseText .= @fgets($fp, 1024);
  155. }
  156. fclose($fp);
  157. $responseText = trim(stristr($responseText,"\r\n\r\n"),"\r\n");
  158. return $responseText;
  159. }
  160. }
  161. }
  162. ?>