/lib/WebDriver/Service/CurlService.php

https://github.com/instaclick/php-webdriver · PHP · 137 lines · 78 code · 27 blank · 32 comment · 10 complexity · 13ca4377e15c6182d5eb7fa9314162bd MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright 2004 Meta Platforms, Inc.
  4. * @license Apache-2.0
  5. *
  6. * @package WebDriver
  7. *
  8. * @author Justin Bishop <jubishop@gmail.com>
  9. */
  10. namespace WebDriver\Service;
  11. use WebDriver\Exception\CurlExec as CurlExecException;
  12. /**
  13. * WebDriver\Service\CurlService class
  14. *
  15. * @package WebDriver
  16. */
  17. class CurlService implements CurlServiceInterface
  18. {
  19. /**
  20. * @var array
  21. */
  22. private $defaultOptions;
  23. /**
  24. * Constructor
  25. *
  26. * @param mixed $defaultOptions
  27. */
  28. public function __construct($defaultOptions = array())
  29. {
  30. $this->defaultOptions = is_array($defaultOptions) ? $defaultOptions : array();
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function execute($requestMethod, $url, $parameters = null, $extraOptions = array())
  36. {
  37. $customHeaders = array(
  38. 'Content-Type: application/json;charset=UTF-8',
  39. 'Accept: application/json;charset=UTF-8',
  40. );
  41. $curl = curl_init($url);
  42. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  43. switch ($requestMethod) {
  44. case 'GET':
  45. break;
  46. case 'POST':
  47. if ($parameters && is_array($parameters)) {
  48. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
  49. } else {
  50. $customHeaders[] = 'Content-Length: 0';
  51. // Suppress "Transfer-Encoding: chunked" header automatically added by cURL that
  52. // causes a 400 bad request (bad content-length).
  53. $customHeaders[] = 'Transfer-Encoding:';
  54. }
  55. // Suppress "Expect: 100-continue" header automatically added by cURL that
  56. // causes a 1 second delay if the remote server does not support Expect.
  57. $customHeaders[] = 'Expect:';
  58. curl_setopt($curl, CURLOPT_POST, true);
  59. break;
  60. case 'DELETE':
  61. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
  62. break;
  63. case 'PUT':
  64. if ($parameters && is_array($parameters)) {
  65. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
  66. } else {
  67. $customHeaders[] = 'Content-Length: 0';
  68. // Suppress "Transfer-Encoding: chunked" header automatically added by cURL that
  69. // causes a 400 bad request (bad content-length).
  70. $customHeaders[] = 'Transfer-Encoding:';
  71. }
  72. // Suppress "Expect: 100-continue" header automatically added by cURL that
  73. // causes a 1 second delay if the remote server does not support Expect.
  74. $customHeaders[] = 'Expect:';
  75. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
  76. break;
  77. }
  78. foreach (array_replace($this->defaultOptions, $extraOptions) as $option => $value) {
  79. curl_setopt($curl, $option, $value);
  80. }
  81. curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
  82. $rawResult = curl_exec($curl);
  83. $rawResult = is_string($rawResult) ? trim($rawResult) : '';
  84. $info = curl_getinfo($curl);
  85. $info['request_method'] = $requestMethod;
  86. $info['errno'] = curl_errno($curl);
  87. $info['error'] = curl_error($curl);
  88. if (array_key_exists(CURLOPT_FAILONERROR, $extraOptions) &&
  89. $extraOptions[CURLOPT_FAILONERROR] &&
  90. CURLE_GOT_NOTHING !== ($errno = curl_errno($curl)) &&
  91. $error = curl_error($curl)
  92. ) {
  93. curl_close($curl);
  94. $e = new CurlExecException(
  95. sprintf(
  96. "Curl error thrown for http %s to %s%s\n\n%s",
  97. $requestMethod,
  98. $url,
  99. $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '',
  100. $error
  101. ),
  102. $errno
  103. );
  104. $e->setCurlInfo($info);
  105. throw $e;
  106. }
  107. curl_close($curl);
  108. return array($rawResult, $info);
  109. }
  110. }