PingppEnvInspect.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Class PingppEnvInspect
  4. * 在使用 Ping++ PHP SDK 前,请执行该文件来检查运行环境是否满足使用 SDK 条件
  5. * Before using Ping++ PHP SDK, please run this file to ensure the runtime environment is supported
  6. */
  7. require dirname(__FILE__) . '/init.php';
  8. class PingppEnvInspect
  9. {
  10. public static function start()
  11. {
  12. $apiBaseArr = parse_url(\Pingpp\Pingpp::$apiBase);
  13. static::$apiHost = $apiBaseArr['host'];
  14. static::versionCheck();
  15. foreach (static::$extFunc as $funcName => $msg)
  16. {
  17. if (!function_exists($funcName))
  18. {
  19. throw new Exception($msg);
  20. }
  21. }
  22. static::domainResolveCheck();
  23. static::connectionCheck();
  24. echo "Your PHP system passed the runtime environment inspection successfully.";
  25. }
  26. private static $extFunc
  27. = array(
  28. "curl_init" => 'Pingpp needs the CURL PHP extension.',
  29. "openssl_sign" => 'Pingpp needs the OpenSSL PHP extension.',
  30. "json_decode" => 'Pingpp needs the JSON PHP extension.',
  31. "mb_detect_encoding" => 'Pingpp needs the Multibyte String PHP extension.',
  32. );
  33. private static $minPingppVersion = "5.3";
  34. private static $apiHost;
  35. private static $apiKey = 'sk_test_ibbTe5jLGCi5rzfH4OqPW9KC';
  36. private static $exampleChargeId = 'ch_uT48KOnvf5aDqjfj58XnzzL4';
  37. private static function versionCheck()
  38. {
  39. $phpVersion = phpversion();
  40. if (version_compare($phpVersion, static::$minPingppVersion, '<'))
  41. {
  42. throw new Exception(sprintf('Your server is running PHP version %1$s but Pingpp version requires %2$s at least.', $phpVersion, static::$minPingppVersion));
  43. }
  44. }
  45. private static function domainResolveCheck()
  46. {
  47. if (gethostbyname(static::$apiHost) == static::$apiHost)
  48. {
  49. throw new Exception(sprintf('Could not resolve %1$s, please check your network or dns settings.', static::$apiHost));
  50. }
  51. }
  52. private static function connectionCheck()
  53. {
  54. try {
  55. \Pingpp\Pingpp::setApiKey(static::$apiKey);
  56. \Pingpp\Charge::retrieve(static::$exampleChargeId);
  57. } catch (Exception $e) {
  58. if ($e instanceof \Pingpp\Error\ApiConnection) {
  59. throw $e;
  60. }
  61. }
  62. }
  63. }
  64. try {
  65. PingppEnvInspect::start();
  66. } catch (Exception $e) {
  67. echo $e->getMessage();
  68. }