Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL with PHP, possible to determine the IP address cURL will use?

Tags:

php

curl

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.

like image 374
Wige Avatar asked Feb 22 '23 23:02

Wige


1 Answers

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.
like image 155
DaveRandom Avatar answered Feb 25 '23 11:02

DaveRandom