From: Louis Jeckel Date: Thu, 27 Aug 2020 17:32:25 +0000 (+0200) Subject: Update GetPageTitleFromUrl.php X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=229a73e54616926b0e2d2d65d1233cd8a5ae0648;p=psq.git Update GetPageTitleFromUrl.php --- diff --git a/app/Invokables/GetPageTitleFromUrl.php b/app/Invokables/GetPageTitleFromUrl.php index bf38881..4b1698d 100644 --- a/app/Invokables/GetPageTitleFromUrl.php +++ b/app/Invokables/GetPageTitleFromUrl.php @@ -9,15 +9,40 @@ class GetPageTitleFromUrl public function __invoke($url): string { + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + + + + $headers = []; + $headers[] = "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"; + $headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; + $headers[] = "Accept-Language: en-US,en;q=0.5"; + $headers[] = "Accept-Encoding: gzip, deflate, br"; + + curl_setopt($ch,CURLOPT_ENCODING , "gzip"); + + curl_setopt($ch, CURLOPT_HTTPHEADER , $headers); + + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $str = curl_exec($ch); + + curl_close($ch); + + $context = stream_context_create( [ "http" => [ - "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36" +// "header" => implode("\r\n", $headers), + "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36", + ] ] ); - $str = file_get_contents($url, false, $context); +// $str = file_get_contents($url, false, $context); if($str !== ''){ $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside preg_match("/<title>(.*)<\/title>/i",$str,$title); // ignore case