Using php -a
$url = "https://getcomposer.org/versions";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
echo curl_exec($ch);
It working fine, returns a JSON string from the URL. Now, continuing in the php -a
terminal interaction:
echo file_get_contents($url);
... After a long wait, say: "PHP Warning: file_get_contents(...): Failed to open stream: Connection timed out in php shell code".
Same error when using all headers:
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("auto_detect_line_endings","0");
ini_set("allow_url_fopen", "1");
ini_set("allow_url_include", "0");
ini_set("user_agent", "Mozilla/5.0");
echo file_get_contents($url);
// ---------------------
// Same (not work!) when using $context, as @sammitch's first try-answer:
$opts = [
'http' => [ 'user_agent' => 'Mozilla/5.0' ],
'https' => [ 'user_agent' => 'Mozilla/5.0' ]
];
$context = stream_context_create($opts);
echo file_get_contents($url,false,$context);
PS: I need file_get_contents for Wordpress, Composer, etc. this question is about "why file_get_contents not working".
My env
php -v
PHP 8.0.30 (cli) (built: May 20 2025 13:31:19) ( NTS gcc x86_64 )
cat /etc/oracle-release
Oracle Linux Server release 9.6
More details by php -i | grep
:
((grep -i "http"))
Registered PHP Streams => https, ftps, compress.zlib, php, file, glob, data, http, ftp, compress.bzip2, phar, zip
HTTP2 => Yes
HTTPS_PROXY => Yes
Protocols => dict, file, ftp, ftps, gopher, gophers, http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
HTTP input encoding translation => disabled
mbstring.http_input => no value => no value
mbstring.http_output => no value => no value
mbstring.http_output_conv_mimetypes => ^(text/|application/xhtml\+xml) => ^(text/|application/xhtml\+xml)
session.cookie_httponly => no value => no value
https_proxy => http://proxy.myIntranet:8080
http_proxy => http://proxy.myIntranet:8080
$_SERVER['https_proxy'] => http://proxy.myIntranet:8080
$_SERVER['http_proxy'] => http://proxy.myIntranet:8080
((grep -i "url"))
/etc/php.d/20-curl.ini,
allow_url_fopen => On => On
allow_url_include => Off => Off
curl
cURL support => enabled
cURL Information => 7.76.1
curl.cainfo => no value => no value
url_rewriter.hosts => no value => no value
url_rewriter.tags => form= => form=
My Proxy
at .bashrc
I has
export http_proxy=http://proxy.MyDomain.br:8080
export https_proxy=http://proxy.MyDomain.br:8080
And cURL not need CURLOPT_PROXYAUTH
etc. as showed above, works with no extra-proxy headers neither passwords.
So, how to use this kind of proxy on file_get_contents()
? Another problem, all examples here on PHP Guide are in the form 'proxy'=>'tcp://etc.etc'
but my proxy not use tcp:
(!).
I tryed this:
$opts = [
'http' => [
'proxy' => 'http://proxy.redecorp.br:8080',
'request_fulluri'=>true,
'user_agent' => 'Mozilla/5.0'
//,'header' => ["Proxy-Authorization: Basic "]
],
'https' => [ // copy of http
'proxy' => 'http://proxy.redecorp.br:8080',
'request_fulluri'=>true,
'user_agent' => 'Mozilla/5.0'
//,'header' => ["Proxy-Authorization: Basic "]
]
];
$context = stream_context_create($opts);
echo file_get_contents($url,false,$context);
NEW ERROR: "PHP Warning: file_get_contents(https://getcomposer.org/versions): Failed to open stream: Unable to find the socket transport "http" - did you forget to enable it when"
allow_url_fopen=On
but, we need a kind ofphp-cli.ini
(where is it?) for PHP-Cli, I am using terminal.allow_url_fopen
weren't enabled.