applebuy.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace applebuy;
  3. class Applebuy
  4. {
  5. private $api_url;
  6. /**
  7. * 构造函数
  8. * @param $sessionKey string 用户在小程序登录后获取的会话密钥
  9. * @param $appid string 小程序的appid
  10. */
  11. public function __construct($api_url)
  12. {
  13. $this->api_url = $api_url;
  14. }
  15. /**
  16. * $params 请求参数
  17. */
  18. function toSendVeify($receiptdata) {
  19. // 构造请求参数
  20. $params["receipt-data"] = $receiptdata;
  21. // random int
  22. $params = json_encode($params);
  23. $result = $this->http_post_json($this->api_url,$params);
  24. if ($result === FALSE) {
  25. return array("code" => 500, "msg" => "file_get_contents failed.");
  26. } else {
  27. return json_decode($result[1], true);
  28. }
  29. }
  30. /**
  31. * @param $url
  32. * @param $jsonStr
  33. * @return array
  34. */
  35. function http_post_json($url, $jsonStr)
  36. {
  37. $ch = curl_init();
  38. curl_setopt($ch, CURLOPT_POST, 1);
  39. curl_setopt($ch, CURLOPT_URL, $url);
  40. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  43. 'Content-Type: application/json; charset=utf-8',
  44. 'Content-Length: ' . strlen($jsonStr)
  45. )
  46. );
  47. $response = curl_exec($ch);
  48. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  49. curl_close($ch);
  50. return array($httpCode, $response);
  51. }
  52. }