You are here

class Client in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/http-kernel/Client.php \Symfony\Component\HttpKernel\Client
  2. 8 vendor/symfony/browser-kit/Client.php \Symfony\Component\BrowserKit\Client
  3. 8 vendor/fabpot/goutte/Goutte/Client.php \Goutte\Client
  4. 8 vendor/guzzlehttp/guzzle/src/Client.php \GuzzleHttp\Client
  5. 8 vendor/behat/mink-goutte-driver/src/Goutte/Client.php \Behat\Mink\Driver\Goutte\Client
Same name and namespace in other branches
  1. 8.0 vendor/fabpot/goutte/Goutte/Client.php \Goutte\Client

Client.

@author Fabien Potencier <fabien.potencier@symfony-project.com> @author Michael Dowling <michael@guzzlephp.org> @author Charles Sarrazin <charles@sarraz.in>

Hierarchy

  • class \Symfony\Component\BrowserKit\Client

Expanded class hierarchy of Client

3 files declare their use of Client
Client.php in vendor/behat/mink-goutte-driver/src/Goutte/Client.php
ClientTest.php in vendor/fabpot/goutte/Goutte/Tests/ClientTest.php
GoutteDriver.php in vendor/behat/mink-goutte-driver/src/GoutteDriver.php

File

vendor/fabpot/goutte/Goutte/Client.php, line 30

Namespace

Goutte
View source
class Client extends BaseClient {
  protected $client;
  private $headers = array();
  private $auth = null;
  public function setClient(GuzzleClientInterface $client) {
    $this->client = $client;
    return $this;
  }
  public function getClient() {
    if (!$this->client) {
      $this->client = new GuzzleClient(array(
        'defaults' => array(
          'allow_redirects' => false,
          'cookies' => true,
        ),
      ));
    }
    return $this->client;
  }
  public function setHeader($name, $value) {
    $this->headers[$name] = $value;
    return $this;
  }
  public function removeHeader($name) {
    unset($this->headers[$name]);
  }
  public function setAuth($user, $password = '', $type = 'basic') {
    $this->auth = array(
      $user,
      $password,
      $type,
    );
    return $this;
  }
  public function resetAuth() {
    $this->auth = null;
    return $this;
  }

  /**
   * @param Request $request
   *
   * @return Response
   */
  protected function doRequest($request) {
    $headers = array();
    foreach ($request
      ->getServer() as $key => $val) {
      $key = strtolower(str_replace('_', '-', $key));
      $contentHeaders = array(
        'content-length' => true,
        'content-md5' => true,
        'content-type' => true,
      );
      if (0 === strpos($key, 'http-')) {
        $headers[substr($key, 5)] = $val;
      }
      elseif (isset($contentHeaders[$key])) {
        $headers[$key] = $val;
      }
    }
    $cookies = CookieJar::fromArray($this
      ->getCookieJar()
      ->allRawValues($request
      ->getUri()), parse_url($request
      ->getUri(), PHP_URL_HOST));
    $requestOptions = array(
      'cookies' => $cookies,
      'allow_redirects' => false,
      'auth' => $this->auth,
    );
    if (!in_array($request
      ->getMethod(), array(
      'GET',
      'HEAD',
    ))) {
      if (null !== ($content = $request
        ->getContent())) {
        $requestOptions['body'] = $content;
      }
      else {
        if ($files = $request
          ->getFiles()) {
          $requestOptions['multipart'] = [];
          $this
            ->addPostFields($request
            ->getParameters(), $requestOptions['multipart']);
          $this
            ->addPostFiles($files, $requestOptions['multipart']);
        }
        else {
          $requestOptions['form_params'] = $request
            ->getParameters();
        }
      }
    }
    if (!empty($headers)) {
      $requestOptions['headers'] = $headers;
    }
    $method = $request
      ->getMethod();
    $uri = $request
      ->getUri();
    foreach ($this->headers as $name => $value) {
      $requestOptions['headers'][$name] = $value;
    }

    // Let BrowserKit handle redirects
    try {
      $response = $this
        ->getClient()
        ->request($method, $uri, $requestOptions);
    } catch (RequestException $e) {
      $response = $e
        ->getResponse();
      if (null === $response) {
        throw $e;
      }
    }
    return $this
      ->createResponse($response);
  }
  protected function addPostFiles(array $files, array &$multipart, $arrayName = '') {
    if (empty($files)) {
      return;
    }
    foreach ($files as $name => $info) {
      if (!empty($arrayName)) {
        $name = $arrayName . '[' . $name . ']';
      }
      $file = [
        'name' => $name,
      ];
      if (is_array($info)) {
        if (isset($info['tmp_name'])) {
          if ('' !== $info['tmp_name']) {
            $file['contents'] = fopen($info['tmp_name'], 'r');
            if (isset($info['name'])) {
              $file['filename'] = $info['name'];
            }
          }
          else {
            continue;
          }
        }
        else {
          $this
            ->addPostFiles($info, $multipart, $name);
          continue;
        }
      }
      else {
        $file['contents'] = fopen($info, 'r');
      }
      $multipart[] = $file;
    }
  }
  public function addPostFields(array $formParams, array &$multipart, $arrayName = '') {
    foreach ($formParams as $name => $value) {
      if (!empty($arrayName)) {
        $name = $arrayName . '[' . $name . ']';
      }
      if (is_array($value)) {
        $this
          ->addPostFields($value, $multipart, $name);
      }
      else {
        $multipart[] = [
          'name' => $name,
          'contents' => $value,
        ];
      }
    }
  }
  protected function createResponse(ResponseInterface $response) {
    return new Response((string) $response
      ->getBody(), $response
      ->getStatusCode(), $response
      ->getHeaders());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Client::$auth private property
Client::$client protected property
Client::$cookieJar protected property
Client::$crawler protected property
Client::$followRedirects protected property
Client::$headers private property
Client::$history protected property
Client::$insulated protected property
Client::$internalRequest protected property
Client::$internalResponse protected property
Client::$isMainRequest private property
Client::$maxRedirects private property
Client::$redirect protected property
Client::$redirectCount private property
Client::$request protected property
Client::$response protected property
Client::$server protected property
Client::addPostFields public function
Client::addPostFiles protected function
Client::back public function Goes back in the browser history.
Client::click public function Clicks on a given link.
Client::createCrawlerFromContent protected function Creates a crawler.
Client::createResponse protected function
Client::doRequest protected function Overrides Client::doRequest
Client::doRequestInProcess protected function Makes a request in another process.
Client::extractHost private function
Client::filterRequest protected function Filters the BrowserKit request to the origin one. 1
Client::filterResponse protected function Filters the origin response to the BrowserKit one. 3
Client::followRedirect public function Follow redirects?
Client::followRedirects public function Sets whether to automatically follow redirects or not.
Client::forward public function Goes forward in the browser history.
Client::getAbsoluteUri protected function Takes a URI and converts it to absolute if it is not already absolute.
Client::getClient public function
Client::getCookieJar public function Returns the CookieJar instance.
Client::getCrawler public function Returns the current Crawler instance.
Client::getHistory public function Returns the History instance.
Client::getInternalRequest public function Returns the current BrowserKit Request instance.
Client::getInternalResponse public function Returns the current BrowserKit Response instance.
Client::getRequest public function Returns the current origin Request instance. 1
Client::getResponse public function Returns the current origin response instance. 1
Client::getScript protected function Returns the script to execute when the request must be insulated. 3
Client::getServerParameter public function Gets single server parameter for specified key.
Client::insulate public function Sets the insulated flag.
Client::reload public function Reloads the current browser.
Client::removeHeader public function
Client::request public function Calls a URI.
Client::requestFromRequest protected function Makes a request from a Request object directly.
Client::resetAuth public function
Client::restart public function Restarts the client.
Client::setAuth public function
Client::setClient public function
Client::setHeader public function
Client::setMaxRedirects public function Sets the maximum number of requests that crawler can follow.
Client::setServerParameter public function Sets single server parameter.
Client::setServerParameters public function Sets server parameters.
Client::submit public function Submits a form.
Client::updateServerFromUri private function
Client::__construct public function Constructor. 1