/includes/Helpers/HttpHelper.php

https://github.com/stwalkerster/waca · PHP · 124 lines · 61 code · 23 blank · 40 comment · 6 complexity · 762eeae9d1d18f4297f8765035863020 MD5 · raw file

  1. <?php
  2. /******************************************************************************
  3. * Wikipedia Account Creation Assistance tool *
  4. * *
  5. * All code in this file is released into the public domain by the ACC *
  6. * Development Team. Please see team.json for a list of contributors. *
  7. ******************************************************************************/
  8. namespace Waca\Helpers;
  9. use Waca\Exceptions\CurlException;
  10. use Waca\SiteConfiguration;
  11. class HttpHelper
  12. {
  13. private $curlHandle;
  14. /**
  15. * HttpHelper constructor.
  16. *
  17. * @param SiteConfiguration $siteConfiguration
  18. * @param string $cookieJar
  19. */
  20. public function __construct($siteConfiguration, $cookieJar = null)
  21. {
  22. $this->curlHandle = curl_init();
  23. curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
  24. curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $siteConfiguration->getUserAgent());
  25. curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true);
  26. if ($siteConfiguration->getCurlDisableVerifyPeer()) {
  27. curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  28. }
  29. if ($cookieJar !== null) {
  30. curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar);
  31. curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar);
  32. }
  33. }
  34. public function __destruct()
  35. {
  36. curl_close($this->curlHandle);
  37. }
  38. /**
  39. * Fetches the content of a URL, with an optional parameter set.
  40. *
  41. * @param string $url The URL to fetch.
  42. * @param null|array $parameters Key/value pair of GET parameters to add to the request.
  43. * Null lets you handle it yourself.
  44. *
  45. * @param array $headers
  46. * @param int $timeout Timeout in ms
  47. *
  48. * @return string
  49. * @throws CurlException
  50. */
  51. public function get($url, $parameters = null, $headers = array(), $timeout = 300000)
  52. {
  53. if ($parameters !== null && is_array($parameters)) {
  54. $getString = '?' . http_build_query($parameters);
  55. $url .= $getString;
  56. }
  57. curl_setopt($this->curlHandle, CURLOPT_URL, $url);
  58. // Make sure we're doing a GET
  59. curl_setopt($this->curlHandle, CURLOPT_POST, false);
  60. curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
  61. curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT_MS, $timeout);
  62. curl_setopt($this->curlHandle, CURLOPT_TIMEOUT_MS, $timeout);
  63. $result = curl_exec($this->curlHandle);
  64. if ($result === false) {
  65. $error = curl_error($this->curlHandle);
  66. throw new CurlException('Remote request failed with error ' . $error);
  67. }
  68. return $result;
  69. }
  70. /**
  71. * Posts data to a URL
  72. *
  73. * @param string $url The URL to fetch.
  74. * @param array $parameters Key/value pair of POST parameters to add to the request.
  75. * @param array $headers
  76. *
  77. * @return string
  78. * @throws CurlException
  79. */
  80. public function post($url, $parameters, $headers = array())
  81. {
  82. curl_setopt($this->curlHandle, CURLOPT_URL, $url);
  83. // Make sure we're doing a POST
  84. curl_setopt($this->curlHandle, CURLOPT_POST, true);
  85. curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters));
  86. curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
  87. $result = curl_exec($this->curlHandle);
  88. if ($result === false) {
  89. $error = curl_error($this->curlHandle);
  90. throw new CurlException('Remote request failed with error ' . $error);
  91. }
  92. return $result;
  93. }
  94. /**
  95. * @return string
  96. */
  97. public function getError()
  98. {
  99. return curl_error($this->curlHandle);
  100. }
  101. }