1

I want to let the php file_get_content function connect to network via system-wide proxy. I tried to add the following lines in /etc/environment:

http_proxy=http://localhost:3128/
HTTP_PROXY=http://localhost:3128/
https_proxy=http://localhost:3128/
HTTPS_PROXY=http://localhost:3128/

But it does not work for file_get_content, although it works for wget. I also tried to add the following lines to /etc/profile:

PROXY_URL="http://localhost:3128/"

export http_proxy="$PROXY_URL"
export https_proxy="$PROXY_URL"
export ftp_proxy="$PROXY_URL"

# For curl
export HTTP_PROXY="$PROXY_URL"
export HTTPS_PROXY="$PROXY_URL"
export FTP_PROXY="$PROXY_URL"

Again, it does not work. I created the following test.php:

<?php
$aContext = array(
    'http' => array(
        'proxy'           => 'tcp://localhost:3128',
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("https://www.google.com", False, $cxContext);

echo $sFile;

This script does work. But since I cannot modify all the source code that contains file_get_contents, I need a system-wide proxy. How can I do it?

peter
  • 93
  • 13

1 Answers1

1

file_get_contents requires manual context setup, either as shown, or by calling stream_context_set_default with proxy settings.

However, many PHP applications use libcurl, which makes use of common proxy environment variables. To set them up, create a drop-in in /etc/php/7.4/fpm/pool.d/www-env.conf (for PHP 7.4 on Debian, please adjust for CentOS):

env[ALL_PROXY]=http://localhost:3128/
env[NO_PROXY]=*.excluded.tld
madman_xxx
  • 198
  • 6
  • It seems I cannot use a uniform method to let all the internet traffic go through a proxy. I wonder how VPN can do this, i.e., it can let all traffic through VPN. – peter Nov 18 '22 at 09:02