fsocketopen via proxy server?

Discussion in 'PHP' started by Squiggs, Apr 8, 2013.

  1. #1
    Hey guys,

    I need to mofidy the following code to communicate via a proxy server, can anyone please advise on how to do this?"

    function fetchURL( $url )
    {
        $url_parsed = parse_url( $url );
        $host = $url_parsed['host'];
        $port = $url_parsed['port'];
        if ( $port == 0 )
        {
            $port = 80;
        }
        $path = $url_parsed['path'];
        if ( $url_parsed['query'] != "" )
        {
            $path .= "?".$url_parsed['query'];
        }
        $out = "GET ".$path." HTTP/1.0\r\nHost: {$host}\r\n\r\n";
        $fp = @fsockopen( @$host, $port, &$errno, &$errstr, 30 );
        if ( $fp )
        {
            $in = "Unknown";
            return $in;
        }
        fwrite( $fp, $out );
        $body = FALSE;
        while ( !feof( $fp ) )
        {
            $s = fgets( $fp, 1024 );
            if ( $body )
            {
                $in .= $s;
            }
            if ( $s == "\r\n" )
            {
                $body = TRUE;
            }
        }
        fclose( $fp );
        return $in;
    }
    PHP:
     
    Squiggs, Apr 8, 2013 IP
  2. Squiggs

    Squiggs Member

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #2
    Any PHP pro's out there know what I need to change here?
     
    Squiggs, Apr 8, 2013 IP
  3. gusto

    gusto Well-Known Member

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    It's very easy. Just change this part:

    $fp=@fsockopen(@$host,$port,&$errno,&$errstr,30)

    With your proxy IP and port and leave the rest of the code as is. It will work.

     
    gusto, Apr 8, 2013 IP