How to use CURLOPT_INTERFACE to make requests via multiple IP addresses

I have multiple IP's on my public CentOS box, and can make requests via all of them using PHP / cURL in the form
curl_setopt($ch, CURLOPT_INTERFACE, "");

Open in new window

However, I want to pass in the relevant IP dynamically. When I try in the form:

curl_setopt($ch, CURLOPT_INTERFACE, $currentIP);

Open in new window

Where $currentIP contains the IP address ( it fails.

Any suggestions?

Who is Participating?

Improve company productivity with a Business Account.Sign Up

Gatherer_HadeConnect With a Mentor Commented:
Make sure the ip address matches one that is bound to one of your network cards.  

Also, print out or otherwise inspect the value of the $currentIP variable.  I suspect it does not contain what you think it does.

oldmoondogAuthor Commented:

Thanks for your response.
I have already printed out $currentIP value when debugging the script, and it's exactly what I expected.

When I hard code the IP into the       

curl_setopt($ch, CURLOPT_INTERFACE, "");

it works fine.

Therefore I concluded it must be something to do with how I'm passing the value into the curl_setopt statement.

Have tried

No joy.

oldmoondogAuthor Commented:
You're right! Rechecked.
Previous test was not part of complete script.
Somehow $currentIP is empty!
oldmoondogAuthor Commented:
To clarify, even once I had confirmed that $currentIP contained the correct value, it still didn't work.
The solution that works is to define the IP as a constant each time the script is called.

define("CURRENT_IP", $arrIPs[0]);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.