Pimcore\Tool::getHttpClient PHP Method

getHttpClient() public static method

public static getHttpClient ( string $type = "Zend_Http_Client", array $options = [] ) : Zend_Http_Client
$type string
$options array
return Zend_Http_Client
    public static function getHttpClient($type = "Zend_Http_Client", $options = [])
    {
        $config = Config::getSystemConfig();
        $clientConfig = $config->httpclient->toArray();
        $clientConfig["adapter"] = isset($clientConfig["adapter"]) && !empty($clientConfig["adapter"]) ? $clientConfig["adapter"] : "Zend_Http_Client_Adapter_Socket";
        $clientConfig["maxredirects"] = isset($options["maxredirects"]) ? $options["maxredirects"] : 2;
        $clientConfig["timeout"] = isset($options["timeout"]) ? $options["timeout"] : 3600;
        $type = empty($type) ? "Zend_Http_Client" : $type;
        $type = "\\" . ltrim($type, "\\");
        if (self::classExists($type)) {
            $client = new $type(null, $clientConfig);
            // workaround/for ZF (Proxy-authorization isn't added by ZF)
            if ($clientConfig['proxy_user']) {
                $client->setHeaders('Proxy-authorization', \Zend_Http_Client::encodeAuthHeader($clientConfig['proxy_user'], $clientConfig['proxy_pass'], \Zend_Http_Client::AUTH_BASIC));
            }
        } else {
            throw new \Exception("Pimcore_Tool::getHttpClient: Unable to create an instance of {$type}");
        }
        return $client;
    }

Usage Example

示例#1
1
 public function proxyAction()
 {
     if ($this->getParam("url")) {
         header("Content-Type: application/javascript");
         $client = Tool::getHttpClient();
         $client->setUri($this->getParam("url"));
         try {
             $response = $client->request(\Zend_Http_Client::GET);
             if ($response->isSuccessful()) {
                 echo $this->getParam("callback") . "(" . \Zend_Json::encode("data:" . $response->getHeader("Content-Type") . ";base64," . base64_encode($response->getBody())) . ");";
             } else {
                 throw new \Exception("Invalid response");
             }
         } catch (\Exception $e) {
             echo $this->getParam("callback") . "(" . \Zend_Json::encode("error:Application error") . ")";
         }
     }
     exit;
 }
All Usage Examples Of Pimcore\Tool::getHttpClient