php 卷曲IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1301319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:56:28  来源:igfitidea点击:

cURL ip address

phpcurl

提问by Flatline

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

我需要使用用户的 IP 地址而不是服务器地址发送 curl 请求。我没有运气就试过这个:

curl_setopt( $ch, CURLOPT_INTERFACE, $ip );

Any ideas?

有任何想法吗?

Thanks!

谢谢!

回答by Flatline

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 请求的 ip 地址,但我发现了一种不安全的方法,它取决于接收请求的服务器脚本,但它对我来说欺骗了我发出请求的 API到:

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!

感谢大家的帮助!

回答by Joseph Montanez

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:

它对我来说不适用于 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;
    }
}

回答by Sergiu Sandrean

None of upper solutions has worked for me. However making a request through a proxy works very well:

没有一个上层解决方案对我有用。但是,通过代理发出请求非常有效:

$url = 'http://dynupdate.no-ip.com/ip.php';
$proxy = '127.0.0.1:8888'; //put your proxy here
//$proxyauth = 'user:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

echo $result;

回答by Richard Sim?es

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

欺骗 IP 地址不是 cURL 可以做到的。这是一个需要操纵原始套接字连接的较低级别的操作。

回答by Sameer Girolkar

here are the lines of php code which may work. You may use any other method to set "X-Forwarded-For" header.

以下是可能有效的 php 代码行。您可以使用任何其他方法来设置“X-Forwarded-For”标头。

$httpClient = new Zend_Http_Client($reqUrl);
$httpClient->setHeaders("X-Forwarded-For","127.0.0.1");  //---this sets the desired ip address

回答by Zed

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

那是因为您应该将服务器的 IP 地址放在那里。

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

您不能使用 curl 伪造具有虚假源地址的 IP 数据包。

回答by iamnamrud

Use HTTP_X_REAL_IP header in addition with HTTP_X_FORWARDED_FOR and REMOTE_ADDR like "HTTP_X_REAL_IP: xxx.xxx.xxx.xx"

除了 HTTP_X_FORWARDED_FOR 和 REMOTE_ADDR 之外,还使用 ​​HTTP_X_REAL_IP 标头,例如“HTTP_X_REAL_IP: xxx.xxx.xxx.xx”

回答by Codix SA - Ivelin Vasilev

Use it into the header add tags like REMOTE_ADDR: majbase HTTP_X_FORWARDED_FOR: codix like "HTTP_X_REAL_IP: xxx.xxx.xxx.xx"

使用它在标题中添加标签,如 REMOTE_ADDR: majbase HTTP_X_FORWARDED_FOR: codix like "HTTP_X_REAL_IP: xxx.xxx.xxx.xx"