"Echte" URL herauskriegen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • "Echte" URL herauskriegen

    Hallo,

    hat jemand von Euch 'ne Idee?

    Z.B. die hier: "http://ts.faz.net/ts/h.aspx" endet letztlich auf "http://www.faz.net/s/homepage.html". Durch welche Methode oder über wieviele Ecken ist unbekannt. Aber wie kann ich per Script herausfinden, dass

    wenn ich nur "http://ts.faz.net/ts/h.aspx" letztlich "http://www.faz.net/s/homepage.html" dahintersteckt?


    Grüße,

    ehw

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von einheitswurzel ()

  • Hab einiges an Code gefunden, dass diese Funktion bewerkstellig. Hab's in 'ne Klasse gepackt, vielleicht interessiert das ja noch jemanden:

    Quellcode

    1. class RealUrl {
    2. /**
    3. * get_redirect_url()
    4. * Gets the address that the provided URL redirects to,
    5. * or FALSE if there's no redirect.
    6. *
    7. * @param string $url
    8. * @return string
    9. */
    10. public function get_redirect_url($url){
    11. $redirect_url = null;
    12. $url_parts = @parse_url($url);
    13. if (!$url_parts) return false;
    14. if (!isset($url_parts['host'])) return false; //can't process relative URLs
    15. if (!isset($url_parts['path'])) $url_parts['path'] = '/';
    16. $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
    17. if (!$sock) return false;
    18. $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
    19. $request .= 'Host: ' . $url_parts['host'] . "\r\n";
    20. $request .= "Connection: Close\r\n\r\n";
    21. fwrite($sock, $request);
    22. $response = '';
    23. while(!feof($sock)) $response .= fread($sock, 8192);
    24. fclose($sock);
    25. if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
    26. if ( substr($matches[1], 0, 1) == "/" )
    27. return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
    28. else
    29. return trim($matches[1]);
    30. } else {
    31. return false;
    32. }
    33. }
    34. /**
    35. * get_all_redirects()
    36. * Follows and collects all redirects, in order, for the given URL.
    37. *
    38. * @param string $url
    39. * @return array
    40. */
    41. public function get_all_redirects($url) {
    42. $redirects = array();
    43. while ($newurl = $this->get_redirect_url($url)) {
    44. if (in_array($newurl, $redirects)) {
    45. break;
    46. }
    47. $redirects[] = $newurl;
    48. $url = $newurl;
    49. }
    50. return $redirects;
    51. }
    52. /**
    53. * get_final_url()
    54. * Gets the address that the URL ultimately leads to.
    55. * Returns $url itself if it isn't a redirect.
    56. *
    57. * @param string $url
    58. * @return string
    59. */
    60. public function get_final_url($url){
    61. $redirects = $this->get_all_redirects($url);
    62. if (count($redirects)>0){
    63. return array_pop($redirects);
    64. } else {
    65. return $url;
    66. }
    67. }
    68. }
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von einheitswurzel ()