Curl.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace PHPHtmlParser;
  3. use PHPHtmlParser\Exceptions\CurlException;
  4. /**
  5. * Class Curl
  6. *
  7. * @package PHPHtmlParser
  8. */
  9. class Curl implements CurlInterface
  10. {
  11. /**
  12. * A simple curl implementation to get the content of the url.
  13. *
  14. * @param string $url
  15. * @return string
  16. * @throws CurlException
  17. */
  18. public function get(string $url): string
  19. {
  20. $ch = curl_init($url);
  21. if ( ! ini_get('open_basedir')) {
  22. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  23. }
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  25. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  26. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  27. curl_setopt($ch, CURLOPT_VERBOSE, true);
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  29. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36');
  30. curl_setopt($ch, CURLOPT_URL, $url);
  31. $content = curl_exec($ch);
  32. if ($content === false) {
  33. // there was a problem
  34. $error = curl_error($ch);
  35. throw new CurlException('Error retrieving "'.$url.'" ('.$error.')');
  36. }
  37. return $content;
  38. }
  39. }