AlipayMobilePublicMultiMediaClient.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. public function buildGetUrl($query = array())
  83. {
  84. if (!is_array($query)) {
  85. //exit;
  86. }
  87. //排序参数,
  88. $data = $this->buildQuery($query);
  89. // 私钥密码
  90. $passphrase = '';
  91. $key_width = 64;
  92. //私钥
  93. $privateKey = $this->privateKey;
  94. $p_key = array();
  95. //如果私钥是 1行
  96. if (!stripos($privateKey, "\n")) {
  97. $i = 0;
  98. while ($key_str = substr($privateKey, $i * $key_width, $key_width)) {
  99. $p_key[] = $key_str;
  100. $i++;
  101. }
  102. } else {
  103. //echo '一行?';
  104. }
  105. $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . implode("\n", $p_key);
  106. $privateKey = $privateKey . "\n-----END RSA PRIVATE KEY-----";
  107. //私钥
  108. $private_id = openssl_pkey_get_private($privateKey, $passphrase);
  109. // 签名
  110. $signature = '';
  111. if ("RSA2" == $this->sign_type) {
  112. openssl_sign($data, $signature, $private_id, OPENSSL_ALGO_SHA256);
  113. } else {
  114. openssl_sign($data, $signature, $private_id, OPENSSL_ALGO_SHA1);
  115. }
  116. openssl_free_key($private_id);
  117. //加密后的内容通常含有特殊字符,需要编码转换下
  118. $signature = base64_encode($signature);
  119. $signature = urlencode($signature);
  120. //$signature = 'XjUN6YM1Mc9HXebKMv7GTLy7gmyhktyOgKk2/Jf+cz4DtP6udkzTdpkjW2j/Z4ZSD7xD6CNYI1Spz4yS93HPT0a5X9LgFWYY8SaADqe+ArXg+FBSiTwUz49SE//Xd9+LEiIRsSFkbpkuiGoO6mqJmB7vXjlD5lx6qCM3nb41wb8=';
  121. $out = $data . '&' . $this->SIGN . '=' . $signature;
  122. return $out;
  123. }
  124. public function buildQuery($query)
  125. {
  126. if (!$query) {
  127. return null;
  128. }
  129. //将要 参数 排序
  130. ksort($query);
  131. //重新组装参数
  132. $params = array();
  133. foreach ($query as $key => $value) {
  134. $params[] = $key . '=' . $value;
  135. }
  136. $data = implode('&', $params);
  137. return $data;
  138. }
  139. /*
  140. * 查询参数排序 a-z
  141. * */
  142. /**
  143. *
  144. * @param $request
  145. * @return text | bin
  146. */
  147. public function execute($header = '', $body = '', $httpCode = '')
  148. {
  149. $exe = new AlipayMobilePublicMultiMediaExecute($header, $body, $httpCode);
  150. return $exe;
  151. }
  152. }