2

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"

10
  • I can't reproduce this problem. Commented Sep 19 at 22:06
  • Are you running this frequently? They could be rate-limiting you. Commented Sep 19 at 22:07
  • Since the problem is not reproducible, I suggest you use wireshark to compare the requests between curl and file_get_contents. Commented Sep 20 at 7:04
  • Hi @KenLee, my php.ini using allow_url_fopen=On but, we need a kind of php-cli.ini (where is it?) for PHP-Cli, I am using terminal. Commented Sep 20 at 12:49
  • 2
    @KenLee You'd get a different error if allow_url_fopen weren't enabled. Commented Sep 20 at 20:49

2 Answers 2

1

(... after 10 days, many comments and one try-answer)

Nobody says it, but the commented example here shows that we should use tcp:// in the proxy protocol, while cURL accepts http... So this minimal script fixed the error:

$url = 'https://getcomposer.org/versions';
$opts = [
 'http' => [ 'proxy' => 'tcp://proxy.myIntranet:8080' ]
];
$context = stream_context_create($opts);
echo file_get_contents($url,false,$context);
Sign up to request clarification or add additional context in comments.

Comments

0

There is no such INI var as user_agent.

There is a stream context option of that name, though:

$opts = [
    'http' => [
        'user_agent' => 'Mozilla/5.0'
    ]
];

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/', false, $context);

Ref:

3 Comments

How would that solve the time-out issue?
There is such an INI var, just in your second ref link, it says "By default the user_agent php.ini setting is used."
Hi @Sammich, thanks... But sorry, it is not working. Again PHP-cURL is running but file_get_contents not. I try also add ,'https' => [..same..] but also not work. I included more ENV information, using php -i, see question-text.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.