Score:1

How to configure a system wide proxy for php file_get_contents on CentOS?

sa flag

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?

in flag
https://stackoverflow.com/questions/3892635/how-to-have-php-to-use-proxy-setting-to-connect-to-internet
Score:1
in flag

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
peter avatar
sa flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.