小编Laz*_*Guy的帖子

PHP:使用单例设置客户端基本 URL

所以,我想用单例创建一个客户端基本 URL。

这是我的 GuzzleClient.php,其中包含基本 URL

<?php

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class GuzzleClient {
    public static function getClient()
    {
        static $client = null;
        if (null === $client) 
        {
            $client = new Client([
                'base_url' => 'http://localhost:8080/task_manager/v1/',
            ]);
        }

        return $client;
    }


    private function __construct() 
    {}
}
Run Code Online (Sandbox Code Playgroud)

这是我应该放置基本网址的地方

require_once 'GuzzleClient.php';

class CardTypeAPIAccessor 
{

    private $client;

    public function __construct($client) 
    {
        $this->client = $client;
    }

    public function getCardTypes() {
        $cardTypes = array();

        try 
        {
            //this is where base URL should be
            $response = $client->get('admin/card/type',
                ['headers' …
Run Code Online (Sandbox Code Playgroud)

php singleton

5
推荐指数
1
解决办法
1185
查看次数

标签 统计

php ×1

singleton ×1