select outgoing ip for curl request select outgoing ip for curl request curl curl

select outgoing ip for curl request


You may want to try setting the CURLOPT_INTERFACE option:

curl_setopt($curlh, CURLOPT_INTERFACE, "xxx.xxx.xxx.xxx");

CURLOPT_INTERFACE: The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.

From: php Manual: curl_setopt


EDIT: Fixing example, as @Michael Hart pointed out pointed out in the other answer.


Since I can't reply yet, just wanted to add to Daniel Vassallo's answer. While he is correct, his example is not.

CURLOPT_INTERFACE is a constant, and can't be placed within quotes. This could cause a bit of confusion to some who may copy and paste only to find out that it doesn't work. The proper code would be:

curl_setopt($curlh, CURLOPT_INTERFACE, "xxx.xxx.xxx.xxx");

Also, for Linux systems (and I'm sure Windows, but it wont be exactly the same), I feel like pointing out that you don't have to use the IP address. If you know the ethN interface, you can simply use "eth0", "eth1", or "eth0:0" depending on how your network is configured. This may be more preferable, since the code will not be specific to 1 machine, and might fit into a more broad configuration (for example, clusters).