AlipayMobilePublicMultiMediaClient.php 5.1 KB

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