cURL用PHP,可以确定cURL将使用的IP地址? [英] cURL with PHP, possible to determine the IP address cURL will use?

查看:112
本文介绍了cURL用PHP,可以确定cURL将使用的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以以编程方式确定连接到远程服务器时使用的IP地址cURL是什么?我有一个共享服务器,我使用cURL,我需要发送IP地址作为请求的一部分。



我正在谈论的服务器需要一个组合连接IP地址和旋转密码(例如我的$ code = SHA1($ _ SERVER ['SERVER_ADDR']。$ passcode)的验证字符串在它们的末尾与SHA1($ _ SERVER ['REMOTE_ADDR']。$ passcode) )。当从cURL的传出连接使用与存储在$ _SERVER ['SERVER_ADDR']中相同的IP时,这种方法很好,但是cURL使用的IP地址现在不同并周期性地旋转。

 <$ c $您可以这样做,以获取您的公开IP地址:

c> $ ch = curl_init('http://whatismyip.org/');
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);
$ myIp = curl_exec($ ch);

但是:




  • 如果您的公开IP有时在cURL会话之间发生变化,则没有什么可以说明它在上述会话和下一个实际需要此数据的会话之间不会改变。

  • 依赖于实际运行的 whatismyip.org (或类似服务)。


Is it possible to determine programmatically what IP address cURL is using when connecting to a remote server? I have a shared server that I am using cURL on, and I need to send the IP address as part of the request.

The server that I am talking to requires an authentication string that combines the connection IP address and a rotating passcode (eg my $code = SHA1($_SERVER['SERVER_ADDR'] . $passcode) gets compared on their end with SHA1($_SERVER['REMOTE_ADDR'] . $passcode)). This worked fine when the outgoing connection from cURL was using the same IP as is stored in $_SERVER['SERVER_ADDR'], however the IP address used by cURL is now different and rotating periodically.

解决方案

You can do this to get your public IP before you call:

$ch = curl_init('http://whatismyip.org/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$myIp = curl_exec($ch);

However:

  • If your public IP sometimes changes between cURL sessions, there is nothing to say that it wont change between the above session and the next one for which you actually need this data.
  • It relies on whatismyip.org (or similar service) actually being running.

这篇关于cURL用PHP,可以确定cURL将使用的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆