diff options
author | Phil Burton <phil@d3r.com> | 2019-06-11 14:46:00 +0100 |
---|---|---|
committer | Phil Burton <phil@d3r.com> | 2019-06-11 14:46:00 +0100 |
commit | 77d70c27da45ef10bc901a0975a5f24a578665c9 (patch) | |
tree | 4f42e418e62c19c3c7605c7bcadf8a1797a7cbf9 /src/Curl.php | |
parent | 5ddc2b35b37f02270015626c7e3d3876dd0eca21 (diff) |
Add custom curlInterface implementation and comment out the verbose option
Diffstat (limited to 'src/Curl.php')
-rw-r--r-- | src/Curl.php | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Curl.php b/src/Curl.php new file mode 100644 index 0000000..61acaa0 --- /dev/null +++ b/src/Curl.php @@ -0,0 +1,48 @@ +<?php + +namespace App; + +use PHPHtmlParser\Exceptions\CurlException; +use PHPHtmlParser\CurlInterface; + +/** + * Class Curl + * + * @package PHPHtmlParser + */ +class Curl implements CurlInterface +{ + + /** + * A simple curl implementation to get the content of the url. + * + * @param string $url + * @return string + * @throws CurlException + */ + public function get(string $url): string + { + $ch = curl_init($url); + + if (! ini_get('open_basedir')) { + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + } + + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + // curl_setopt($ch, CURLOPT_VERBOSE, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + 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'); + curl_setopt($ch, CURLOPT_URL, $url); + + $content = curl_exec($ch); + if ($content === false) { + // there was a problem + $error = curl_error($ch); + throw new CurlException('Error retrieving "'.$url.'" ('.$error.')'); + } + + return $content; + } +} |