ScriptFUSION\Porter\Net\Http\HttpOptions::extractHttpContextOptions PHP Method

extractHttpContextOptions() public method

Extracts a list of HTTP context options only.
See also: http://php.net/manual/en/context.http.php
public extractHttpContextOptions ( ) : array
return array HTTP context options.
    public function extractHttpContextOptions()
    {
        return array_intersect_key($this->copy(), array_flip(['method', 'header', 'user_agent', 'content', 'proxy', 'request_fulluri', 'follow_location', 'max_redirects', 'protocol_version', 'timeout', 'ignore_errors']));
    }

Usage Example

Esempio n. 1
0
 /**
  * {@inheritdoc}
  *
  * @param string $source Source.
  * @param EncapsulatedOptions|null $options Optional. Options.
  *
  * @return string Response.
  *
  * @throws \InvalidArgumentException Options is not an instance of HttpOptions.
  * @throws HttpConnectionException Failed to connect to source.
  * @throws HttpServerException Server sent an error code.
  */
 public function fetchFreshData($source, EncapsulatedOptions $options = null)
 {
     if ($options && !$options instanceof HttpOptions) {
         throw new \InvalidArgumentException('Options must be an instance of HttpOptions.');
     }
     return \ScriptFUSION\Retry\retry($this->getTries(), function () use($source, $options) {
         if (false === ($response = @file_get_contents($this->getOrCreateUrlBuilder()->buildUrl($source, $options ? $options->getQueryParameters() : [], $this->getBaseUrl()), false, stream_context_create(['http' => ['ignore_errors' => true] + array_merge($this->options->extractHttpContextOptions(), $options ? $options->extractHttpContextOptions() : [])])))) {
             $error = error_get_last();
             throw new HttpConnectionException($error['message'], $error['type']);
         }
         $code = explode(' ', $http_response_header[0], 3)[1];
         if ($code < 200 || $code >= 400) {
             throw new HttpServerException("HTTP server responded with error: \"{$http_response_header['0']}\".\n\n{$response}");
         }
         return $response;
     }, new ExponentialBackoffErrorHandler());
 }