No Description

AlipayMobilePublicMultiMediaClient.php 4.9KB

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