AbstractStream.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Buzz\Client;
  3. use Buzz\Converter\HeaderConverter;
  4. use Buzz\Converter\RequestConverter;
  5. use Buzz\Message\RequestInterface;
  6. use Psr\Http\Message\RequestInterface as Psr7RequestInterface;
  7. abstract class AbstractStream extends AbstractClient
  8. {
  9. /**
  10. * Converts a request into an array for stream_context_create().
  11. *
  12. * @param Psr7RequestInterface|RequestInterface $request A request object
  13. *
  14. * @return array An array for stream_context_create()
  15. */
  16. public function getStreamContextArray($request)
  17. {
  18. $request = RequestConverter::psr7($request);
  19. $headers = $request->getHeaders();
  20. unset($headers['Host']);
  21. $options = array(
  22. 'http' => array(
  23. // values from the request
  24. 'method' => $request->getMethod(),
  25. 'header' => implode("\r\n", HeaderConverter::toBuzzHeaders($headers)),
  26. 'content' => $request->getBody()->__toString(),
  27. 'protocol_version' => $request->getProtocolVersion(),
  28. // values from the current client
  29. 'ignore_errors' => $this->getIgnoreErrors(),
  30. 'follow_location' => $this->getMaxRedirects() > 0,
  31. 'max_redirects' => $this->getMaxRedirects() + 1,
  32. 'timeout' => $this->getTimeout(),
  33. ),
  34. 'ssl' => array(
  35. 'verify_peer' => $this->getVerifyPeer(),
  36. 'verify_host' => $this->getVerifyHost(),
  37. ),
  38. );
  39. if ($this->proxy) {
  40. $options['http']['proxy'] = $this->proxy;
  41. $options['http']['request_fulluri'] = true;
  42. }
  43. return $options;
  44. }
  45. }