/** * long_url * * Expand a shortened URL to its original, long form * * @param string short_url * @return mixed */ public function long_url($short_url) { $curlh = curl_init(); curl_setopt($curlh, CURLOPT_URL, $short_url); curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curlh, CURLOPT_TIMEOUT, 10); curl_setopt($curlh, CURLOPT_USERAGENT, 'LongURL API'); curl_setopt($curlh, CURLOPT_REFERER, 'http://longurl.org'); curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, false); curl_setopt($curlh, CURLOPT_NOBODY, false); $response = curl_exec($curlh); if (!$response) { $error = curl_error($curlh); if (strpos($error, 'timed out') !== false) { Glucose::instance()->response->addHeader('HTTP/1.1 504 Gateway Timeout', true, 504); error_log($error . ':' . $short_url); Glucose::fatalError('Connection timeout'); } else { Glucose::instance()->response->addHeader('HTTP/1.1 400 Bad Request', true, 400); error_log($error . ':' . $short_url); Glucose::fatalError('Could not expand URL. Please check that you have submitted a valid URL.'); } } curl_close($curlh); $doc = new DOMDocument(); @$doc->loadHTML($response); $elms = $doc->getElementsByTagName('frame'); if ($elms->length > 0) { return trim($elms->item(1)->getAttribute('src')); } return false; }
/** * long_url * * Expand a shortened URL to its original, long form * * @param string short_url * @return mixed */ public function long_url($short_url) { $curlh = curl_init(); curl_setopt($curlh, CURLOPT_URL, $short_url); curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curlh, CURLOPT_TIMEOUT, 10); curl_setopt($curlh, CURLOPT_USERAGENT, 'LongURL API'); curl_setopt($curlh, CURLOPT_REFERER, 'http://longurl.org'); curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, false); curl_setopt($curlh, CURLOPT_NOBODY, false); $response = curl_exec($curlh); if (!$response) { $error = curl_error($curlh); if (strpos($error, 'timed out') !== false) { Glucose::instance()->response->addHeader('HTTP/1.1 504 Gateway Timeout', true, 504); error_log($error . ':' . $short_url); Glucose::fatalError('Connection timeout'); } else { Glucose::instance()->response->addHeader('HTTP/1.1 400 Bad Request', true, 400); error_log($error . ':' . $short_url); Glucose::fatalError('Could not expand URL. Please check that you have submitted a valid URL.'); } } curl_close($curlh); preg_match_all('#<frame src="([^"]+)"#i', $response, $matches); if (isset($matches[1]) && isset($matches[1][1])) { return trim($matches[1][1]); } return false; }
/** * long_url * * Expand a shortened URL to its original, long form * * @param string short_url * @return mixed */ public function long_url($short_url) { $curlh = curl_init(); curl_setopt($curlh, CURLOPT_URL, $short_url); curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curlh, CURLOPT_TIMEOUT, 10); curl_setopt($curlh, CURLOPT_USERAGENT, 'LongURL API'); curl_setopt($curlh, CURLOPT_REFERER, 'http://longurl.org'); curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curlh); if (!$response) { $error = curl_error($curlh); if (strpos($error, 'timed out') !== false) { Glucose::instance()->response->addHeader('HTTP/1.1 504 Gateway Timeout', true, 504); error_log($error . ':' . $short_url); Glucose::fatalError('Connection timeout'); } else { Glucose::instance()->response->addHeader('HTTP/1.1 400 Bad Request', true, 400); error_log($error . ':' . $short_url); Glucose::fatalError('Could not expand URL. Please check that you have submitted a valid URL.'); } } curl_close($curlh); $doc = new DOMDocument(); @$doc->loadHTML($response); $xpath = new DOMXpath($doc); $elms = $xpath->query("//meta[@http-equiv='refresh']"); if ($elms->length > 0) { preg_match('#url=(.*)$#i', $elms->item(0)->getAttribute('content'), $matches); return trim($matches[1]); } return false; }
public function __construct() { $this->GC = Glucose::instance(); }
/** * long_url * * Expand a shortened URL to its original, long form * * @param string short_url * @return mixed */ public function long_url($short_url) { $curlh = curl_init(); curl_setopt($curlh, CURLOPT_URL, $short_url); curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curlh, CURLOPT_TIMEOUT, 10); curl_setopt($curlh, CURLOPT_USERAGENT, 'LongURL API'); curl_setopt($curlh, CURLOPT_REFERER, 'http://longurl.org'); curl_setopt($curlh, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_MAXREDIRS, 10); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, true); // Include header in output curl_setopt($curlh, CURLOPT_NOBODY, true); // Don't include body in output $response = curl_exec($curlh); $info = curl_getinfo($curlh); if ($info['http_code'] === 405) { // HEAD requests aren't allowed $curlh = curl_init(); curl_setopt($curlh, CURLOPT_URL, $short_url); curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curlh, CURLOPT_TIMEOUT, 10); curl_setopt($curlh, CURLOPT_USERAGENT, 'LongURL API'); curl_setopt($curlh, CURLOPT_REFERER, 'http://longurl.org'); curl_setopt($curlh, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_MAXREDIRS, 10); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, true); // Include header in output $response = curl_exec($curlh); $info = curl_getinfo($curlh); } if (!$response) { $error = curl_error($curlh); if (strpos($error, 'timed out') !== false) { Glucose::instance()->response->addHeader('HTTP/1.1 504 Gateway Timeout', true, 504); error_log($error . ':' . $short_url); Glucose::fatalError('Connection timeout'); } else { Glucose::instance()->response->addHeader('HTTP/1.1 400 Bad Request', true, 400); error_log($error . ':' . $short_url); Glucose::fatalError('Could not expand URL. Please check that you have submitted a valid URL.'); } } curl_close($curlh); if ($response) { // Cache content-type Memcached::instance()->set('content-type:' . $info['url'], $info['content_type']); // Extract and cache all redirects preg_match_all('#Location:\\s?([^\\s]+)#i', $response, $matches); Memcached::instance()->set('all-redirects:' . $short_url, $matches[1]); // Cache response-code Memcached::instance()->set('response-code:' . $info['url'], $info['http_code']); return $info['url']; } else { return false; } }