Check if URL exists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • theboss3@gmail.com

    Check if URL exists

    (I know that's a horrible title; I couldn't think of anything better.)

    Is there a simple way to check if a URL exists in PHP without using
    Curl?

  • Ewoud Dronkert

    #2
    Re: Check if URL exists

    theboss3@gmail. com wrote:[color=blue]
    > Is there a simple way to check if a URL exists in PHP without using
    > Curl?[/color]

    Try http://php.net/fsockopen or http://php.net/fopen

    --
    E. Dronkert

    Comment

    • theboss3@gmail.com

      #3
      Re: Check if URL exists

      Thanks! I wasn't sure if fsockopen would be good enough or not, but I
      think it will be. I just need to catch the error.

      That being said, is there a way to retrieve headers from another page,
      like getheaders('htt p://example.com/') ?

      Comment

      • Ewoud Dronkert

        #4
        Re: Check if URL exists

        theboss3@gmail. com wrote:[color=blue]
        > That being said, is there a way to retrieve headers from another page,
        > like getheaders('htt p://example.com/') ?[/color]

        Open a socket, stop reading after the header, close socket.

        --
        E. Dronkert

        Comment

        • theboss3@gmail.com

          #5
          Re: Check if URL exists

          Is this (http://simon.incutio.com/archive/200...conditionalGet)
          what you're talking about?

          Comment

          • VS

            #6
            Re: Check if URL exists

            theboss3@gmail. com wrote:[color=blue]
            > Thanks! I wasn't sure if fsockopen would be good enough or not, but I
            > think it will be. I just need to catch the error.
            >
            > That being said, is there a way to retrieve headers from another page,
            > like getheaders('htt p://example.com/') ?[/color]

            I'm quite new to PHP, but I found and adpated this for getting the Time
            Stamp of a URL, perhaps you can change it for what you need:

            function filemtime_remot e($uri)
            {
            $uri = parse_url($uri) ;
            $uri['port'] = isset($uri['port']) ? $uri['port'] : 80;

            // TimeOut
            $tout = 10;
            $handle = @fsockopen($uri['host'], $uri['port'], $errno, $errstr,
            $tout);
            if(!$handle)
            return 0;

            fputs($handle," HEAD $uri[path] HTTP/1.1\r\nHost: $uri[host]\r\n\r\n");
            $result = 0;
            while(!feof($ha ndle))
            {
            $line = fgets($handle,1 024);
            if(!trim($line) )
            break;

            $col = strpos($line,': ');
            if($col !== false)
            {
            $header = trim(substr($li ne,0,$col));
            $value = trim(substr($li ne,$col+1));
            if(strtolower($ header) == 'last-modified')
            {
            $result = strtotime($valu e);
            break;
            }
            }
            }
            fclose($handle) ;
            return $result;
            }

            --
            VS

            Comment

            Working...