tags:

views:

1940

answers:

7
+1  Q: 

cURL ip address

Hi, I need to send a curl request with the user's ip address not the server one. I tried this with no luck:

curl_setopt( $ch, CURLOPT_INTERFACE, $ip );

Any ideas?

Thanks!

A: 

That's because you are supposed to put your server's ip address there.

You cannot forge an IP packet with fake source address using curl.

Zed
A: 

You want to spoof the IP address? That's not going to work.

Greg
+2  A: 

Spoofing an IP address is not something cURL can do. That's a lower-level operation requiring manipulation of raw socket connections.

BipedalShark
+3  A: 

Ok, so there's no way to safely spoof the ip address of a curl request, but I found a non-safe way, it depends on the server script receiving the request, but it worked for me to trick the API I was making the request to:

curl_setopt( $ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));

This won't always work, but in this case it worked for me.

Thanks everyone for the help!

xNephilimx
That's not technically spoofing an IP address, it's just spoofing an HTTP header. This is a prime example as to why you cannot rely on HTTP header validity.
zombat
yep, I know, but it may sometimes serve the purpose.
xNephilimx
+1  A: 

It doesn't work with curl for me so i found a way around it, I just had to do this and as long as the IP is assigned to your server, then:

echo http_socket::download('http://something.com', '55.55.44.33');

final class http_socket
{
    static public function download($url, $bind_ip = false)
    { 
        $components = parse_url($url);
        if(!isset($components['query'])) $components['query'] = false;

        if(!$bind_ip) 
        {
            $bind_ip = $_SERVER['SERVER_ADDR'];
        }

        $header = array();
        $header[] = 'GET ' . $components['path'] . ($components['query'] ?  '?' . $components['query'] : '');
        $header[] = 'Host: ' . $components['host'];
        $header[] = 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.7) Gecko/20100106 Ubuntu/9.10 (karmic) Firefox/3.5.7';
        $header[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
        $header[] = 'Accept-Language: en-us,en;q=0.5';
        $header[] = 'Accept-Encoding: gzip,deflate';
        $header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
        $header[] = 'Keep-Alive: 300';
        $header[] = 'Connection: keep-alive';
        $header = implode("\n", $header) . "\n\n";
        $packet = $header;

        //----------------------------------------------------------------------
        // Connect to server
        //----------------------------------------------------------------------
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_bind($socket, $bind_ip);
        socket_connect($socket, $components['host'], 80);

        //----------------------------------------------------------------------
        // Send First Packet to Server
        //----------------------------------------------------------------------
        socket_write($socket, $packet);
        //----------------------------------------------------------------------
        // Receive First Packet to Server
        //----------------------------------------------------------------------
        $html = '';
        while(1) {
            socket_recv($socket, $packet, 4096, MSG_WAITALL);
            if(empty($packet)) break;
            $html .= $packet;
        }
        socket_close($socket);

        return $html;
    }
}
Gorilla3D
A: 

@Gorrilla3D:

Does this work???

phpnewbie
It works just fine for me.
Gorilla3D
A: 

Doesnt work it, i dont undertstand, can anyone help me?

Atasözleri