I wrote a small script for testing a feature on my project, and it works just fine.
<?php
$username = 'exportcsv';
$password = 'exportcsv';
$context = \stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . \base64_encode("$username:$password"),
'timeout' => 2
)
));
$content = \file_get_contents('http://theurl', false, $context);
var_dump($content);
The url is in fact a hardcoded Symfony route, which return me a CSV text string.
But when I do the same on a Controller I get:
Warning: file_get_contents(http://myurl): failed to open stream: HTTP request failed! (http://myurl): failed to open stream: HTTP request failed!
Whetever I use file()
or file_get_contents()
the same hardcoded url or a absolute path generate with:
$url = $this->generateUrl('the_route_name', array(
'variable' => $variable
), true);
EDIT: Maybe it's a important thing to notice, I'm behind a company proxy, so I add in the context array 'proxy' => 'http://proxy.mycompany.com:3128'
and now I got : failed to open stream: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?
Same with or without 'request_fulluri' => true,
or 'request_fulluri' => false,