AlipayMobilePublicMultiMediaClient.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * 多媒体文件客户端
  4. * @author yikai.hu
  5. * @version $Id: AlipayMobilePublicMultiMediaClient.php, v 0.1 Aug 15, 2014 10:19:01 AM yikai.hu Exp $
  6. */
  7. //namespace alipay\api ;
  8. include("AlipayMobilePublicMultiMediaExecute.php");
  9. class AlipayMobilePublicMultiMediaClient
  10. {
  11. private $DEFAULT_CHARSET = 'UTF-8';
  12. private $METHOD_POST = "POST";
  13. private $METHOD_GET = "GET";
  14. private $SIGN = 'sign'; //get name
  15. private $timeout = 10;// 超时时间
  16. private $serverUrl;
  17. private $appId;
  18. private $privateKey;
  19. private $prodCode;
  20. private $format = 'json'; //todo
  21. private $sign_type = 'RSA'; //todo
  22. private $charset;
  23. private $apiVersion = "1.0";
  24. private $apiMethodName = "alipay.mobile.public.multimedia.download";
  25. private $media_id = "L21pZnMvVDFQV3hYWGJKWFhYYUNucHJYP3Q9YW13ZiZ4c2lnPTU0MzRhYjg1ZTZjNWJmZTMxZGJiNjIzNDdjMzFkNzkw575";
  26. //此处写死的,实际开发中,请传入
  27. private $connectTimeout = 3000;
  28. private $readTimeout = 15000;
  29. function __construct($serverUrl = '', $appId = '', $partner_private_key = '', $format = '', $charset = 'GBK')
  30. {
  31. $this->serverUrl = $serverUrl;
  32. $this->appId = $appId;
  33. $this->privateKey = $partner_private_key;
  34. $this->format = $format;
  35. $this->charset = $charset;
  36. }
  37. /**
  38. * getContents 获取网址内容
  39. * @param $request
  40. * @return text | bin
  41. */
  42. public function getContents()
  43. {
  44. //自己的服务器如果没有 curl,可用:fsockopen() 等
  45. //1:
  46. //2: 私钥格式
  47. $datas = array(
  48. "app_id" => $this->appId,
  49. "method" => $this->METHOD_POST,
  50. "sign_type" => $this->sign_type,
  51. "version" => $this->apiVersion,
  52. "timestamp" => date('Y-m-d H:i:s'),//yyyy-MM-dd HH:mm:ss
  53. "biz_content" => '{"mediaId":"' . $this->media_id . '"}',
  54. "charset" => $this->charset
  55. );
  56. //要提交的数据
  57. $data_sign = $this->buildGetUrl($datas);
  58. $post_data = $data_sign;
  59. //初始化 curl
  60. $ch = curl_init();
  61. //设置目标服务器
  62. curl_setopt($ch, CURLOPT_URL, $this->serverUrl);
  63. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  65. //超时时间
  66. curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  67. if ($this->METHOD_POST == 'POST') {
  68. // post数据
  69. curl_setopt($ch, CURLOPT_POST, 1);
  70. // post的变量
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  72. }
  73. $output = curl_exec($ch);
  74. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  75. curl_close($ch);
  76. echo $output;
  77. //分离头部
  78. //list($header, $body) = explode("\r\n\r\n", $output, 2);
  79. $datas = explode("\r\n\r\n", $output, 2);
  80. $header = $datas[0];
  81. if ($httpCode == '200') {
  82. $body = $datas[1];
  83. } else {
  84. $body = '';
  85. }
  86. return $this->execute($header, $body, $httpCode);
  87. }
  88. /**
  89. *
  90. * @param $request
  91. * @return text | bin
  92. */
  93. public function execute($header = '', $body = '', $httpCode = '')
  94. {
  95. $exe = new AlipayMobilePublicMultiMediaExecute($header, $body, $httpCode);
  96. return $exe;
  97. }
  98. public function buildGetUrl($query = array())
  99. {
  100. if (!is_array($query)) {
  101. //exit;
  102. }
  103. //排序参数,
  104. $data = $this->buildQuery($query);
  105. // 私钥密码
  106. $passphrase = '';
  107. $key_width = 64;
  108. //私钥
  109. $privateKey = $this->privateKey;
  110. $p_key = array();
  111. //如果私钥是 1行
  112. if (!stripos($privateKey, "\n")) {
  113. $i = 0;
  114. while ($key_str = substr($privateKey, $i * $key_width, $key_width)) {
  115. $p_key[] = $key_str;
  116. $i++;
  117. }
  118. } else {
  119. //echo '一行?';
  120. }
  121. $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . implode("\n", $p_key);
  122. $privateKey = $privateKey . "\n-----END RSA PRIVATE KEY-----";
  123. // echo "\n\n私钥:\n";
  124. // echo( $privateKey );
  125. // echo "\n\n\n";
  126. //私钥
  127. $private_id = openssl_pkey_get_private($privateKey, $passphrase);
  128. // 签名
  129. $signature = '';
  130. if ("RSA2" == $this->sign_type) {
  131. openssl_sign($data, $signature, $private_id, OPENSSL_ALGO_SHA256);
  132. } else {
  133. openssl_sign($data, $signature, $private_id, OPENSSL_ALGO_SHA1);
  134. }
  135. openssl_free_key($private_id);
  136. //加密后的内容通常含有特殊字符,需要编码转换下
  137. $signature = base64_encode($signature);
  138. $signature = urlencode($signature);
  139. //$signature = 'XjUN6YM1Mc9HXebKMv7GTLy7gmyhktyOgKk2/Jf+cz4DtP6udkzTdpkjW2j/Z4ZSD7xD6CNYI1Spz4yS93HPT0a5X9LgFWYY8SaADqe+ArXg+FBSiTwUz49SE//Xd9+LEiIRsSFkbpkuiGoO6mqJmB7vXjlD5lx6qCM3nb41wb8=';
  140. $out = $data . '&' . $this->SIGN . '=' . $signature;
  141. // echo "\n\n 加密后:\n";
  142. // echo( $out );
  143. // echo "\n\n\n";
  144. return $out;
  145. }
  146. /*
  147. * 查询参数排序 a-z
  148. * */
  149. public function buildQuery($query)
  150. {
  151. if (!$query) {
  152. return null;
  153. }
  154. //将要 参数 排序
  155. ksort($query);
  156. //重新组装参数
  157. $params = array();
  158. foreach ($query as $key => $value) {
  159. $params[] = $key . '=' . $value;
  160. }
  161. $data = implode('&', $params);
  162. return $data;
  163. }
  164. }