OOP面向对象形式的初使化配置
init.php里:
1 <?php 2 3 use ElemeOpenApi\Config\Config; 4 5 define("BASE_DIR", dirname(__FILE__) . "/"); 6 define("ROOT_DIR", dirname(__FILE__) . "/../"); 7 require BASE_DIR . "../vendor/autoload.php"; 8 require ROOT_DIR . "vendor/autoload.php"; 9 10 //此处需要填写对应的参数 11 $app_key = ""; 12 $app_secret = ""; 13 $sandbox = true; 14 15 $scope = "all"; 16 $callback_url = ""; 17 18 19 $config = new Config($app_key, $app_secret, $sandbox);
Config.php里
1 <?php 2 3 namespace ElemeOpenApi\Config; 4 5 use InvalidArgumentException; 6 7 class Config 8 { 9 private $app_key; 10 private $app_secret; 11 private $sandbox; 12 13 private $request_url; 14 15 private $log; 16 17 private $default_request_url = "https://open-api.shop.ele.me"; 18 private $default_sandbox_request_url = "https://open-api-sandbox.shop.ele.me"; 19 20 public function __construct($app_key, $app_secret, $sandbox) 21 { 22 if ($sandbox == false) { 23 $this->request_url = $this->default_request_url; 24 } elseif ($sandbox == true) { 25 $this->request_url = $this->default_sandbox_request_url; 26 } else { 27 throw new InvalidArgumentException("the type of sandbox should be a boolean"); 28 } 29 30 if ($app_key == null || $app_key == "") { 31 throw new InvalidArgumentException("app_key is required"); 32 } 33 34 if ($app_secret == null || $app_secret == "") { 35 throw new InvalidArgumentException("app_secret is required"); 36 } 37 38 $this->app_key = $app_key; 39 $this->app_secret = $app_secret; 40 $this->sandbox = $sandbox; 41 } 42 43 public function get_app_key() 44 { 45 return $this->app_key; 46 } 47 48 public function get_app_secret() 49 { 50 return $this->app_secret; 51 } 52 53 public function get_request_url() 54 { 55 return $this->request_url; 56 } 57 58 public function set_request_url($request_url) 59 { 60 $this->request_url = $request_url; 61 } 62 63 public function get_log() 64 { 65 return $this->log; 66 } 67 68 public function set_log($log) 69 { 70 if (!method_exists($log, "info")) { 71 throw new InvalidArgumentException("logger need have method 'info(\$message)'"); 72 } 73 if (!method_exists($log, "error")) { 74 throw new InvalidArgumentException("logger need have method 'error(\$message)'"); 75 } 76 $this->log = $log; 77 } 78 }
[Haima的博客]
http://www.cnblogs.com/haima/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构