Sdk.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * PHP for BaiDuSDK
  4. *
  5. * @version 1.0
  6. * @author 91
  7. */
  8. header("Content-type: text/html; charset=utf-8");
  9. if (!function_exists('curl_init')) {
  10. exit('您的PHP没有安装 配置cURL扩展,请先安装配置cURL,具体方法可以上网查。');
  11. }
  12. if (!function_exists('json_decode')) {
  13. exit('您的PHP不支持JSON,请升级您的PHP版本。');
  14. }
  15. class Sdk
  16. {
  17. public $AppId = 10942568; //应用开发者appid
  18. public $Secretkey = 'IiYGwTtNYNAitaIjyKxfj6LahKenzG6z'; //应用开发者Secretkey
  19. public $AppKey = 'wUEZqEtGstOMIU0UrdRobBeA';
  20. //
  21. //订单查询接口地址
  22. private $OrderQueryUrl = "http://querysdkapi.91.com/CpOrderQuery.ashx";
  23. //登陆状态查询接口地址
  24. private $LoginStateUrl = "http://querysdkapi.91.com/CpLoginStateQuery.ashx";
  25. function __construct()
  26. {
  27. }
  28. /**
  29. * 执行查询登陆状态的API调用,返回结果数组
  30. *
  31. * @param string $accessToken 客户端SDK返回的登陆令牌
  32. * @return array 结果数组
  33. */
  34. public function login_state_result($accessToken)
  35. {
  36. //生成Sign
  37. $Sign = md5($this->AppId . $accessToken . $this->Secretkey);
  38. //把需要传送的参数拼接成字符串
  39. $SourceStr = "AppID=" . $this->AppId . "&AccessToken=" . $accessToken . "&Sign=" . $Sign;
  40. $Params = trim($SourceStr);
  41. // 发起请求
  42. $Res = $this->request($this->LoginStateUrl, $Params, 'post');
  43. if (false === $Res['result']) {
  44. $ResultArray = array(
  45. 'res' => $Res['errno'],
  46. 'msg' => $Res['msg'],
  47. );
  48. }
  49. $ResultArray = json_decode($Res['msg'], true);
  50. // 远程返回的不是 json 格式, 说明返回包有问题
  51. if (is_null($ResultArray)) {
  52. $ResultArray = array(
  53. 'res' => false,
  54. 'msg' => $Res['msg']
  55. );
  56. }
  57. return $ResultArray;
  58. }
  59. /**
  60. * 执行查询支付购买结果的API调用,返回结果数组
  61. *
  62. * @param string $CooperatorOrderSerial 商户订单号
  63. * @return array 结果数组
  64. */
  65. public function query_order_result($CooperatorOrderSerial)
  66. {
  67. $Action = 10002;
  68. $OrderType = 1;
  69. //生成Sign
  70. $Sign = md5($this->AppId . $CooperatorOrderSerial . $this->Secretkey);
  71. //把需要传送的参数拼接成字符串
  72. $SourceStr = "AppID=" . $this->AppId . "&Action=" . $Action . "&CooperatorOrderSerial=" . $CooperatorOrderSerial . "&Sign=" . $Sign . "&OrderType=" . $OrderType;
  73. $Params = trim($SourceStr);
  74. // 发起请求
  75. $Res = $this->request($this->OrderQueryUrl, $Params, 'post');
  76. if (false === $Res['result']) {
  77. $ResultArray = array(
  78. 'res' => $Res['errno'],
  79. 'msg' => $Res['msg'],
  80. );
  81. }
  82. $ResultArray = json_decode($Res['msg'], true);
  83. // 远程返回的不是 json 格式, 说明返回包有问题
  84. if (is_null($ResultArray)) {
  85. $ResultArray = array(
  86. 'res' => false,
  87. 'msg' => $Res['msg']
  88. );
  89. }
  90. return $ResultArray;
  91. }
  92. /**
  93. * MD5签名
  94. * @param type $ResultCode
  95. * @param type $Content
  96. * @return type
  97. */
  98. public function SignMd5($ResultCode, $Content)
  99. {
  100. return md5($this->AppId . $ResultCode . $Content . $this->Secretkey);
  101. }
  102. /**
  103. * 执行一个 HTTP 请求
  104. *
  105. * @param string $Url 执行请求的Url
  106. * @param mixed $Params 表单参数
  107. * @param string $Method 请求方法 post / get
  108. * @return array 结果数组
  109. */
  110. public function request($Url, $Params, $Method = 'post')
  111. {
  112. $Curl = curl_init(); //初始化curl
  113. if ('get' == $Method) {//以GET方式发送请求
  114. curl_setopt($Curl, CURLOPT_URL, "$Url?$Params");
  115. } else {//以POST方式发送请求
  116. curl_setopt($Curl, CURLOPT_URL, $Url);
  117. curl_setopt($Curl, CURLOPT_POST, 1); //post提交方式
  118. curl_setopt($Curl, CURLOPT_POSTFIELDS, $Params); //设置传送的参数
  119. }
  120. curl_setopt($Curl, CURLOPT_HEADER, false); //设置header
  121. curl_setopt($Curl, CURLOPT_RETURNTRANSFER, true); //要求结果为字符串且输出到屏幕上
  122. curl_setopt($Curl, CURLOPT_CONNECTTIMEOUT, 3); //设置等待时间
  123. $Res = curl_exec($Curl); //运行curl
  124. $Err = curl_error($Curl);
  125. if (false === $Res || !empty($Err)) {
  126. $Errno = curl_errno($Curl);
  127. $Info = curl_getinfo($Curl);
  128. curl_close($Curl);
  129. return array(
  130. 'result' => false,
  131. 'errno' => $Errno,
  132. 'msg' => $Err,
  133. 'info' => $Info,
  134. );
  135. }
  136. curl_close($Curl); //关闭curl
  137. return array(
  138. 'result' => true,
  139. 'msg' => $Res,
  140. );
  141. }
  142. /**
  143. * 向SDK server返回结果
  144. * @param type $err
  145. * @param type $msg
  146. */
  147. function ret($err, $msg)
  148. {
  149. $Result["AppID"] = $this->AppId;
  150. $Result["ResultCode"] = $err;
  151. $Result["ResultMsg"] = urlencode($msg);
  152. $Result["Sign"] = $this->SignMd5($err, "");
  153. $Result["Content"] = "";
  154. $Res = json_encode($Result);
  155. exit(urldecode($Res));
  156. }
  157. }