0

Is this the correct way to use CURL (via PHP) to issue requests with a proxy?

$url ="https://www.website.com";
$ch = curl_init($url);     
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);       
curl_setopt($ch, CURLOPT_PROXY, "Proxy:Port");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);
antrez770
  • 13
  • 8
  • The option for the `HTTPPROXYTUNNEL` is not necessary in this case if you just simply want to curl through proxy server... And yes you are right and try to give it a go... – gitguddoge Nov 27 '18 at 08:55
  • Btw it is better to put one more option which is `FOLLOWLOCATION` and set it to true just in case there are internal redirect action on the target curl location – gitguddoge Nov 27 '18 at 08:56
  • Lisbeth. Thank you – antrez770 Nov 27 '18 at 10:02
  • see guzzle and then `$client->request('GET', '/', ['proxy' => 'tcp://localhost:8125']);` – Robert Nov 27 '18 at 10:03
  • @gitguddoge, I add some test: $loadtime = time(); $curl_info = curl_getinfo($ch); echo "cURL time: " . (time() - $loadtime) . "s
    \ "; if ($response === false) { echo "cURL Error: ", curl_error($ch); } else { echo $response; } //obtain: cURL time: 1s \ cURL Error: TCP connection reset by peer
    – antrez770 Nov 27 '18 at 10:04
  • Robert, I have no access to server to install guzzle – antrez770 Nov 27 '18 at 10:07
  • Honestly I never encounter this error before but you may take a look at this [curl error](https://stackoverflow.com/questions/10285700/curl-error-recv-failure-connection-reset-by-peer-php-curl) – gitguddoge Nov 27 '18 at 10:15
  • @gitguddoge I saw this page before, but my IT told me that the problem with my code, and no problem in Firewall or server enable options in this case, is it possible? – antrez770 Nov 27 '18 at 10:23
  • For me I would check up for the possible validation upon the target request site such as necessary headers, post fields, or other information... try to execute and observe the response data using `curl_exec` in the network tab rather than using `curl_getinfo`... Hope this helps – gitguddoge Nov 27 '18 at 10:26
  • I tried to check with curl_getinfo as I mentioned in previous reply, and I obtain: cURL Error: TCP connection reset by peer. Also I tested my request to same webpage without proxy from another web server and it is ok. – antrez770 Nov 27 '18 at 11:06
  • Is exist any way to check if proxy settings allowed me to access threw? – antrez770 Nov 28 '18 at 06:48
  • I tested the proxy via browser it is work, can be any reason to Firewall bloking the CURL GET reguest? – antrez770 Nov 28 '18 at 09:03

0 Answers0