How to scrape a photo from a Twitter Photo URL using PHP
After explaining how to scrape a photo from a Twitter Photo URL using C# (ASP.Net) I decided to port the script PHP so I could use it on WordPress.
I ended up using cURL and a regular expression to do the trick. The result is a redirect to the actual found photo. Just paste the code into a new PHP file and call it with the short code from the Twitter URL as a query string parameter (like: http://example.com/tweet.php?ecVE2qSScL).
<?php //init curl $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //construct url $url = 'http://t.co/' . $_SERVER['QUERY_STRING']; curl_setopt($ch, CURLOPT_URL, $url); //load data $data = curl_exec($ch); curl_close($ch); //extract PhotoUrl with regex preg_match('/data-url="(?<PhotoUrl>[^"]+)"/', $data, $matches); header('Location: ' . $matches['PhotoUrl'], true, 302); ?>
By default this script will return the large image. If you want a smaller image, change the regular expression to ‘/data-url=”(?<PhotoUrl>[^”]:large+)”/’ . The script will redirect to a smaller version.
You might get an extra performance increase by doing a permanent redirect by changing 302 into 301. This kind of redirect will be cached by the browser.