1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP fsockopen to display http header

Discussion in 'PHP' started by sastro, Sep 16, 2008.

  1. #1
    I have this script but it will parse header and body of given web address.

    
    <?php
    $fp = fsockopen("www.microsoft.com", 80, $errno, $errstr, 30);
    if (!$fp) {
       echo "$errstr ($errno)<br />\n";
    } else {
       $out = "GET / HTTP/1.1\r\n";
       $out .= "Host: www.example.com\r\n";
       $out .= "Connection: Close\r\n\r\n";
       fwrite($fp, $out);
       while (!feof($fp)) {
           echo fgets($fp, 28);
       }
       fclose($fp);
    }
    ?>
    
    PHP:
    I just need to get the header like this

    HTTP/1.1 302 Found
    Cache-Control: private
    Content-Type: text/html; charset=utf-8
    Location: /en/us/default.aspx
    Server: Microsoft-IIS/7.0
    X-AspNet-Version: 2.0.50727
    P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
    X-Powered-By: ASP.NET
    Date: Tue, 16 Sep 2008 23:49:52 GMT
    Connection: keep-alive
    Content-Length: 142
    Code (markup):
    Please help me
     
    sastro, Sep 16, 2008 IP
  2. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #2
    happpy, Sep 17, 2008 IP
  3. sastro

    sastro Well-Known Member

    Messages:
    214
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    OK, done. thx anyway
     
    sastro, Sep 17, 2008 IP
  4. NoNameNoFortune

    NoNameNoFortune Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Simple.
    
    <?
      error_reporting( E_ALL );
      echo HTTP_Get_Header("microsoft.com");
      exit;
    
    
      function HTTP_Get_Header( $url ) {
      	$get_timeout   = 40;
        $s_Complete    = parse_url($url);
        if( !isset($s_Complete["scheme"] ) ) {
          $s_Complete["host"] = $s_Complete["path"];
          $s_Complete["path"] = '';
        }
        $s_Host        = $s_Complete["host"];
        if( @$s_Complete["path"] == "" )
          $s_Complete["path"] = "/?";
        $s_URI         = $s_Complete["path"];
        if( @$s_Complete["query"] != "" )
          $s_URI      .= '?'.$s_Complete['query'];
        if( @$s_Complete["port"] != "" )
          $s_Port      = $s_Complete["port"];
        else
          $s_Port      = 80;
        $request       = "GET $s_URI HTTP/1.0\r\n";
        $request      .= "Accept: */*\r\n";
        $request      .= "Cache-Control: no-cache\r\n";
        $request      .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.2) Gecko/2008090514 Firefox/3.0.2\r\n";
        $request      .= "Host: $s_Host\r\n";
        $request      .= "Connection: Close\r\n";
        $request      .= "\r\n";
        $fp = @fsockopen( $s_Host, $s_Port, $errno, $errstr, 30 );
        if( is_resource($fp) ) {
          fputs( $fp, $request );
          $query_timeout = 30;
          stream_set_blocking( $fp, true );
          stream_set_timeout( $fp, $query_timeout ); 
          $loop_time = time();
          $status = socket_get_status( $fp );
          $line = "";
          $header = "";
          while( !($line == "\r\n")  && !feof($fp) && !$status['timed_out'] ) {
            $line = fgets($fp, 4096);
            $header .= $line;
            $diff = time() - $loop_time;
            if( $diff > $get_timeout )
              break;
            if( connection_aborted() )
              break;
            $status = socket_get_status( $fp );
          }
          fclose( $fp );
          return $header;
        }
        return false;
      }
    
    ?>
    PHP:
    =;-)
     
    NoNameNoFortune, Sep 17, 2008 IP