CurlService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright 2004-2017 Facebook. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * @package WebDriver
  18. *
  19. * @author Justin Bishop <jubishop@gmail.com>
  20. * @author Anthon Pang <apang@softwaredevelopment.ca>
  21. * @author Fabrizio Branca <mail@fabrizio-branca.de>
  22. */
  23. namespace WebDriver\Service;
  24. use WebDriver\Exception as WebDriverException;
  25. /**
  26. * WebDriver\Service\CurlService class
  27. *
  28. * @package WebDriver
  29. */
  30. class CurlService implements CurlServiceInterface
  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. }
  52. // Suppress "Expect: 100-continue" header automatically added by cURL that
  53. // causes a 1 second delay if the remote server does not support Expect.
  54. $customHeaders[] = 'Expect:';
  55. curl_setopt($curl, CURLOPT_POST, true);
  56. break;
  57. case 'DELETE':
  58. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
  59. break;
  60. case 'PUT':
  61. if ($parameters && is_array($parameters)) {
  62. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
  63. } else {
  64. $customHeaders[] = 'Content-Length: 0';
  65. }
  66. // Suppress "Expect: 100-continue" header automatically added by cURL that
  67. // causes a 1 second delay if the remote server does not support Expect.
  68. $customHeaders[] = 'Expect:';
  69. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
  70. break;
  71. }
  72. foreach ($extraOptions as $option => $value) {
  73. curl_setopt($curl, $option, $value);
  74. }
  75. curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
  76. $rawResult = trim(curl_exec($curl));
  77. $info = curl_getinfo($curl);
  78. $info['request_method'] = $requestMethod;
  79. if (array_key_exists(CURLOPT_FAILONERROR, $extraOptions) &&
  80. $extraOptions[CURLOPT_FAILONERROR] &&
  81. CURLE_GOT_NOTHING !== ($errno = curl_errno($curl)) &&
  82. $error = curl_error($curl)
  83. ) {
  84. curl_close($curl);
  85. throw WebDriverException::factory(
  86. WebDriverException::CURL_EXEC,
  87. sprintf(
  88. "Curl error thrown for http %s to %s%s\n\n%s",
  89. $requestMethod,
  90. $url,
  91. $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '',
  92. $error
  93. ),
  94. $errno,
  95. null,
  96. $info
  97. );
  98. }
  99. curl_close($curl);
  100. return array($rawResult, $info);
  101. }
  102. }