SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Getting a forwarding URL?

    If you go to a certain page and it forwards you off elsewhere, is it possible to catch that URL in PHP? For example, if I went to http://domain/index.php and it forwarded me on to a completely different URL, ie http://domain2/default.aspx can I enter the original URL in to PHP and return the second one? And if so, how?

    Cheers all

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sure. Make an HTTP request to the URL and look for a location header. Here's a quick example:

    PHP Code:
    $fp fsockopen("awio.com"80$errno$errstr30);
    if (!
    $fp) {
        echo 
    "Error: $errstr ($errno)<br />\n";
    } else {
        
    $out "GET / HTTP/1.1\r\n";
        
    $out .= "Host: awio.com\r\n";
        
    $out .= "Connection: Close\r\n\r\n";
        
    fwrite($fp$out);
        while (!
    feof($fp)) {
            
    $line fgets($fp2048);
            
    $parts split(": "$line);
            if (
    $parts[0] == "Location") {
                    echo 
    "Redirect to: " $parts[1];
                    break;
            }
        }
        
    fclose($fp);


  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hmm:

    Error: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (0)

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sounds like a restriction set up by your host.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oh well, never mind. Thanks

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Check you haven't used a full address like 'http://www.server.com' when opening the socket, as per Dans example, you just need to use the host.

    Code:
    server.com
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah, that'll most likely be it. I actually found in the end however that if I was to use CURL, and not allow it to follow the headers that I could actually get a load of data that included the bit I need. With preg_match I could get the data I wanted and it worked. Thanks

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If you're using cURL, you can avoid bringing back the source for the page using the NOBODY option. Although, to be fair, a site with a new location header set shouldn't really return a body anyways.

    PHP Code:
    <?php
    $rCurl 
    curl_init('awio.com');
    $aCurlOptions = array(
        
    CURLOPT_HEADER            =>    true,
        
    CURLOPT_NOBODY            =>    true,
        
    CURLOPT_RETURNTRANSFER    =>    true
    );
    curl_setopt_array($rCurl$aCurlOptions);
    echo 
    curl_exec($rCurl);
    /*
        HTTP/1.1 301 Moved Permanently
        Date: Sat, 11 Apr 2009 13:39:40 GMT
        Server: Apache/2.0.63 (CentOS)
        X-Powered-By: PHP/5.1.6
        X-Pingback: http://www.awio.com/xmlrpc.php
        Location: http://www.awio.com/
        Connection: close
        Content-Type: text/html; charset=UTF-8
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A lot of web servers, for legacy reasons, will send a an HTML page redirecting the user to the new page.

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah, good to know sk89q, thanks!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I did think it strange to be honest, but yeah, I did get output. Looking at the output above in your example SBUK, it's pretty much the same thing. I can use it to get the forwarding URL, and it works a treat, thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •