code_junkie

PHP: How to expand/contract Tinyurls

Nov 10th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. GET /dmsfm HTTP/1.0
  2. Host: tinyurl.com
  3.  
  4. HTTP/1.0 301 Moved Permanently
  5. Connection: close
  6. X-Powered-By: PHP/5.2.6
  7. Location: http://en.wikipedia.org/wiki/TinyURL
  8. Content-type: text/html
  9. Content-Length: 0
  10. Date: Mon, 15 Sep 2008 12:29:04 GMT
  11. Server: TinyURL/1.6
  12.  
  13. <?php
  14. $tinyurl="dmsfm";
  15.  
  16. $fp = fsockopen("tinyurl.com", 80, $errno, $errstr, 30);
  17. if (!$fp) {
  18. echo "$errstr ($errno)<br />n";
  19. } else {
  20. $out = "GET /$tinyurl HTTP/1.0rn";
  21. $out .= "Host: tinyurl.comrn";
  22. $out .= "Connection: Closernrn";
  23.  
  24. $response="";
  25.  
  26. fwrite($fp, $out);
  27. while (!feof($fp)) {
  28. $response.=fgets($fp, 128);
  29. }
  30. fclose($fp);
  31.  
  32. //now parse the Location: header out of the response
  33.  
  34. }
  35. ?>
  36.  
  37. function make_tinyurl($longurl)
  38. {
  39. return(implode('', file(
  40. 'http://tinyurl.com/api-create.php?url='.urlencode($longurl))));
  41. }
  42.  
  43. // make an example call
  44. print(make_tinyurl('http://www.joelonsoftware.com/items/2008/09/15.html'));
  45.  
  46. def resolve_tinyurl(url):
  47. key = md5( url.lower_case() )
  48. if cache.has_key(key)
  49. return cache[md5]
  50. else:
  51. resolved = query_tinyurl(url)
  52. cache[key] = resolved
  53. return resolved
  54.  
  55. <?php
  56. function getTinyUrl($url) {
  57. return file_get_contents('http://tinyurl.com/api-create.php?url='.$url);
  58. }
  59. ?>
  60.  
  61. $tinyurl = 'http://tinyurl.com/3fvbx8';
  62. $context = stream_context_create(array('http' => array('method' => 'HEAD')));
  63. $response = file_get_contents($tinyurl, null, $context);
  64.  
  65. $location = '';
  66. foreach ($http_response_header as $header) {
  67. if (strpos($header, 'Location:') === 0) {
  68. $location = trim(strrchr($header, ' '));
  69. break;
  70. }
  71. }
  72. echo $location;
  73. // http://www.pingdom.com/reports/vb1395a6sww3/check_overview/?name=twitter.com%2Fhome
  74.  
  75. function doShortURLDecode($url) {
  76. $ch = @curl_init($url);
  77. @curl_setopt($ch, CURLOPT_HEADER, TRUE);
  78. @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  79. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  80. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  81. $response = @curl_exec($ch);
  82. preg_match('/Location: (.*)n/', $response, $a);
  83. if (!isset($a[1])) return $url;
  84. return $a[1];
  85. }
Add Comment
Please, Sign In to add comment