X-Git-Url: https://git.ao2.it/tweeper.git/blobdiff_plain/fae2bb79789ff07d7cdeb6b609b3fd9b43b107b8..d5d7aa8df557d95334949b11c38e1f9ee2826791:/src/Tweeper.php diff --git a/src/Tweeper.php b/src/Tweeper.php index 10e07e3..f1d579f 100644 --- a/src/Tweeper.php +++ b/src/Tweeper.php @@ -36,7 +36,7 @@ date_default_timezone_set('UTC'); */ class Tweeper { - private static $userAgent = "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J)"; + private static $userAgent = "Mozilla/5.0"; private static $maxConnectionTimeout = 5; private static $maxConnectionRetries = 5; @@ -157,7 +157,7 @@ class Tweeper { /** * Get the contents from a URL. */ - private static function getUrlContents($url) { + private static function getUrlContents($url, $user_agent = NULL) { $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_HEADER => FALSE, @@ -167,7 +167,7 @@ class Tweeper { CURLOPT_COOKIEFILE => "", CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPHEADER => ['Accept-language: en'], - CURLOPT_USERAGENT => Tweeper::$userAgent, + CURLOPT_USERAGENT => isset($user_agent) ? $user_agent : Tweeper::$userAgent, ]); $contents = Tweeper::curlExec($ch); curl_close($ch); @@ -178,7 +178,7 @@ class Tweeper { /** * Get the headers from a URL. */ - private static function getUrlInfo($url) { + private static function getUrlInfo($url, $user_agent = NULL) { $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_HEADER => TRUE, @@ -187,7 +187,7 @@ class Tweeper { // Follow http redirects to get the real URL. CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_RETURNTRANSFER => TRUE, - CURLOPT_USERAGENT => Tweeper::$userAgent, + CURLOPT_USERAGENT => isset($user_agent) ? $user_agent : Tweeper::$userAgent, ]); $ret = Tweeper::curlExec($ch); @@ -403,7 +403,7 @@ class Tweeper { return NULL; } - $stylesheet_contents = Tweeper::getUrlContents($stylesheet); + $stylesheet_contents = file_get_contents($stylesheet); if (FALSE === $stylesheet_contents) { trigger_error("Cannot open $stylesheet", E_USER_WARNING); return NULL; @@ -455,7 +455,15 @@ class Tweeper { return NULL; } - $html = Tweeper::getUrlContents($src_url); + // Override User-Agent for twitter.com to force it to serve the mobile UI. + if ($host == "twitter.com") { + $user_agent = "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J)"; + } + else { + $user_agent = NULL; + } + + $html = Tweeper::getUrlContents($src_url, $user_agent); if (FALSE === $html) { trigger_error("Failed to retrieve $src_url", E_USER_WARNING); return NULL;