Return CURLOPT_INTERFACE to its default behavior in PHP

If you look at the CURLOPT_INTERFACE manual you will find that setting it to NULL to will tell cURL to use whatever the TCP stack finds suitable (default behavior), however that isn’t the case in PHP.

If you set NULL on CURLOPT_INTERFACE using curl_setopt like this:

curl_setopt( $ch, CURLOPT_INTERFACE, null );

You will wind up with Error Code: 45 (Could not resolve host)

That’s because PHP does not support it. According to to curl-setopt manual:

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

CURLOPT_INTERFACE only accepts valid string in PHP. It does not accept NULL, array() or "" (empty string).

Solution:

Set it to 0.0.0.0

curl_setopt( $ch, CURLOPT_INTERFACE, '0.0.0.0' );

Now, cURL will use whatever the TCP stack finds suitable (default behavior).

Why 0.0.0.0 ? – Read https://superuser.com/a/949429/689060

All the credits goes to /u/ta22175 for giving me the solution.

Tags

Share your thoughts by leaving a comment below.