Branden
Branden

Reputation: 237

Getting timeout when using proxies with PHP cURL

Here is the code in question:

$ch = curl_init();
curl_setopt(CURLOPT_URL, 'https://api.ipify.org?format=json');
curl_setopt(CURLOPT_PROXY, 'ip:port');
curl_setopt(CURLOPT_PROXYUSERPWD, 'user:pass');
$result = curl_exec($ch);
echo curl_error($ch);

The proxy and proxyauth I'm using most definitely work. I've actually tried multiple proxies from various sources with and without auth, but every time I get a connection timeout when connecting to the proxy.

Is there a config setting preventing proxies from being used or something that I'm not aware of? Any help here will be greatly appreciated.

Upvotes: 2

Views: 6799

Answers (3)

Branden
Branden

Reputation: 237

I solved this issue and just wanted to update this question.

The problem was with the server configuration. Only the most common outbound ports were enabled, and I was using proxies with random ports across the whole spectrum. Enabling the ports that I needed (you could just have the restriction disabled altogether) fixed the issue.

So if you run into this same issue where you're timing out while trying to connect to a proxy, contact your support and ask about any restrictions on the outbound ports.

Upvotes: 1

Ultimater
Ultimater

Reputation: 4738

Sounds like an HTTPS issue to me. Try:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);

It might also just be a slow URL, so try:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400);//max seconds to allow cURL functions to run

If there's any redirects involved, you can also try:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

If that doesn't work, try testing more URLs and see if it's an https issue with every site you try, and whether you can recreate the same issue with http. Also check make sure you have PHP's error reporting enabled, and see if you're getting any errors or warnings. Try deliberately causing PHP to generate an error or warning to see if error reporting is working correctly.

Upvotes: 1

Yoann Kergall
Yoann Kergall

Reputation: 3312

You need to use CONNECT method for https urls :

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

But some proxies doesn't support this feature.

Upvotes: 3

Related Questions