/lib/Doctrine/Search/Http/Adapter/Curl.php

https://github.com/herrsebi/search · PHP · 149 lines · 107 code · 37 blank · 5 comment · 10 complexity · da72f943b3175190ace96734a8f5448b MD5 · raw file

  1. <?php
  2. namespace Doctrine\Search\Http\Adapter;
  3. use Doctrine\Search\Http\Adapter;
  4. use Doctrine\Search\Http\Adapter\AdapterInvalidArgumentException;
  5. class Curl implements Adapter
  6. {
  7. private $config = array('timeout' => 10,
  8. 'maxredirects' => 3);
  9. private $curlConnection;
  10. private $headers = array('Accept' => '');
  11. private $response;
  12. private $request;
  13. public function __construct()
  14. {
  15. if (!extension_loaded('curl')) {
  16. throw new AdapterInitializationException('The cURL PHP extension has to be installed and loaded to use this Doctrine\Http\Client.');
  17. }
  18. }
  19. public function setConfig(array $config = array())
  20. {
  21. $this->config = array_merge($this->config, $config);
  22. }
  23. public function openConnection($host, $port = 80)
  24. {
  25. $this->curlConnection = curl_init();
  26. if ($port != 80) {
  27. curl_setopt($this->curlConnection, CURLOPT_PORT, intval($port));
  28. }
  29. curl_setopt($this->curlConnection, CURLOPT_CONNECTTIMEOUT, $this->config['timeout']);
  30. curl_setopt($this->curlConnection, CURLOPT_MAXREDIRS, $this->config['maxredirects']);
  31. if (!$this->curlConnection) {
  32. $this->closeConnection();
  33. throw new AdapterExecutionException('Connection to ' . $host . ':' . $port . 'is impossible.');
  34. }
  35. }
  36. public function sendData($method, $url, $headers = array(), $body = '')
  37. {
  38. $this->headers = array_merge($this->headers, $headers);
  39. curl_setopt($this->curlConnection, CURLOPT_URL, $url);
  40. curl_setopt($this->curlConnection, CURL_HTTP_VERSION_1_1, true);
  41. $curlMethod = $this->getCurlMethod($method);
  42. curl_setopt($this->curlConnection, $curlMethod['method'], $curlMethod['methodValue']);
  43. curl_setopt($this->curlConnection, CURLOPT_HEADER, true);
  44. // ensure actual response is returned
  45. curl_setopt($this->curlConnection, CURLOPT_RETURNTRANSFER, true);
  46. curl_setopt($this->curlConnection, CURLOPT_HTTPHEADER, $this->headers);
  47. if(strtoupper($method) == 'POST' || strtoupper($method) == 'PUT')
  48. {
  49. curl_setopt($this->curlConnection, CURLOPT_POSTFIELDS, $body);
  50. }
  51. $this->response = curl_exec($this->curlConnection);
  52. if (empty($this->response)) {
  53. throw new AdapterExecutionException("The cURL request produced an error: " . curl_error($this->curlConnection));
  54. }
  55. $this->request = curl_getinfo($this->curlConnection, CURLINFO_HEADER_OUT);
  56. $this->request .= $body;
  57. }
  58. private function getCurlMethod($method)
  59. {
  60. $curlMethodValue = true;
  61. switch(strtoupper($method))
  62. {
  63. case 'GET':
  64. $curlMethod = CURLOPT_HTTPGET;
  65. break;
  66. case 'POST':
  67. $curlMethod = CURLOPT_POST;
  68. break;
  69. case 'PUT':
  70. $curlMethod = CURLOPT_CUSTOMREQUEST;
  71. $curlMethodValue = "PUT";
  72. break;
  73. case 'DELETE' :
  74. $curlMethod = CURLOPT_CUSTOMREQUEST;
  75. $curlMethodValue = "DELETE";
  76. break;
  77. case 'OPTIONS' :
  78. $curlMethod = CURLOPT_CUSTOMREQUEST;
  79. $curlMethodValue = "OPTIONS";
  80. break;
  81. case 'TRACE' :
  82. $curlMethod = CURLOPT_CUSTOMREQUEST;
  83. $curlMethodValue = "TRACE";
  84. break;
  85. case 'HEAD' :
  86. $curlMethod = CURLOPT_CUSTOMREQUEST;
  87. $curlMethodValue = "HEAD";
  88. break;
  89. default:
  90. throw new AdapterInvalidArgumentException('Method '. strtoupper($method) .' is not supported');
  91. }
  92. return array('method' => $curlMethod, 'methodValue' => $curlMethodValue);
  93. }
  94. public function getRequest()
  95. {
  96. return $this->request;
  97. }
  98. /**
  99. *
  100. * @return String $data;
  101. */
  102. public function readData()
  103. {
  104. return $this->response;
  105. }
  106. public function closeConnection()
  107. {
  108. if(is_resource($this->curlConnection)) {
  109. curl_close($this->curlConnection);
  110. }
  111. }
  112. }