peter
peter

Reputation: 449

Can I let fsockopen go through a proxy without awareness?

The php code is like:

<?php
  $fp = fsockopen("www.google.com",80);
  fputs($fp, "GET / HTTP/1.0\r\n\r\n");
  $data="";
  while (!feof($fp)) $data.=fgets($fp,64000);
  fclose($fp);

I know I can change the code to use a proxy like:

<?php
  $fp = fsockopen("exampleproxy.com",3128);
  fputs($fp, "GET http://www.google.com HTTP/1.0\r\n\r\n");
  $data="";
  while (!feof($fp)) $data.=fgets($fp,64000);
  fclose($fp);

But can I configure a system-wide proxy to let all the internet traffic go through that proxy so that I do not need to change the code? I am on Centos 7.

Upvotes: 0

Views: 103

Answers (1)

flanders
flanders

Reputation: 80

You haven't to use fsock, curl can be easy way rather fsock.

https://www.php.net/manual/en/function.curl-setopt.php

<?php
$url = 'https://google.com';
$proxyauth = 'user:pass';
$proxy = '1.2.3.4';
$proxyPort = '443';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//proxy suport
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
//https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/27.0.1453.94 Safari/537.36");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);

$output = curl_exec($ch);   

if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}

echo $output;

curl_close($ch);
?>

Upvotes: 1

Related Questions