cURL ip address

2022-08-30 16:19:24

我需要发送一个 curl 请求,其中包含用户的 IP 地址,而不是服务器地址。我试了一下,没有运气:

curl_setopt( $ch, CURLOPT_INTERFACE, $ip );

有什么想法吗?


答案 1

好吧,所以没有办法安全地欺骗curl请求的IP地址,但我找到了一种不安全的方法,它取决于接收请求的服务器脚本,但它对我起作用,欺骗了我发出请求的API:

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

这并不总是有效,但在这种情况下,它对我有用。

感谢大家的帮助!


答案 2

它不适用于curl对我来说,所以我找到了一种方法来解决它,我只需要这样做,只要IP分配给你的服务器,那么:

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;
    }
}

推荐