PHP Classes

File: src/Generics/Client/HttpClient.php

Recommend this page to a friend!
  Classes of Maik Greubel   PHP Generics   src/Generics/Client/HttpClient.php   Download  
File: src/Generics/Client/HttpClient.php
Role: Class source
Content type: text/plain
Description: HTTP Client
Class: PHP Generics
Framework for accessing streams, sockets and logs
Author: By
Last change: Update of src/Generics/Client/HttpClient.php
Date: 7 months ago
Size: 1,572 bytes
 

Contents

Class file image Download
<?php

/**
 * This file is part of the PHP Generics package.
 *
 * @package Generics
 */
namespace Generics\Client;

use
Generics\Socket\ClientSocket;
use
Generics\Socket\Url;
use
Generics\Streams\HttpStream;

/**
 * This class implements a HttpStream as client
 *
 * @author Maik Greubel <greubel@nkey.de>
 */
class HttpClient extends ClientSocket implements HttpStream
{
    use
HttpClientTrait;
   
   
/**
     * Whether to use https instead of http
     *
     * @var boolean
     */
   
private $secure;

   
/**
     * Create a new http client
     *
     * @param Url $url
     * The url for http request
     * @param string $proto
     * The protocol to use (default = HTTP/1.1)
     * @param integer $timeout
     * Optional timeout for request (default = 10 seconds)
     */
   
public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10)
    {
       
parent::__construct($url);
       
       
$this->secure = $url->getScheme() == 'https';

       
$this->setTimeout($timeout);
       
$this->setPath($url->getPath());
       
$this->setProtocol($proto);
       
$this->setQueryString($url->getQueryString());
       
$this->reset();
       
$this->resetHeaders();
    }

   
/**
     *
     * {@inheritdoc}
     * @see \Generics\Streams\HttpStream::request()
     */
   
public function request(string $requestType)
    {
        if (
$this->secure) {
            throw new
HttpException("Secure connection using HTTPs is not supported!");
        }
       
       
$this->requestImpl($requestType);
    }
}