1

I have the following code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

if ($use_proxy) {
    $proxies = [
        '(proxy-IP):3128' 
    ];
    $proxy_key = array_rand($proxies);
    $proxy = $proxies[$proxy_key];

    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
}

$html = curl_exec($ch);

But when I run it and try to use curl through a proxy, I get the following error: Failed to connect to (proxy-ip) port 3128 after 1003 ms: Connection refused.

However, the port is fine, and the IP is fine. Obviously there's something missing, but I have been experimenting with variations of the code, and I still cannot connect to the proxy.

1 Answer 1

1

Try CURLOPT_CONNECT_TO instead of the proxy set of commands. https://www.php.net/manual/en/function.curl-setopt.php

curl_setopt($ch, CURLOPT_CONNECT_TO, ["$remote_ip:$remote_port:$proxy_ip:$proxy_port"]);
Sign up to request clarification or add additional context in comments.

2 Comments

That worked. Thanks. $remote_ip in this case is the web provider's IP.
In my use case, I had a SSH tunnel which was binding a local ip and port to a remote ip and port. The remote ip and port was exposed to local calls because of this. When the call was outgoing I needed to use the remote ip in the url, but connect to the local ip in process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.