Reputation: 506
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,
Upvotes: 0
Views: 2025
Reputation: 506
I find the solution. I was using the PHP build-in server through the symfony command : app/console server:run (bin/console server:run since 2.8+)
.
So when context of the file_get_contents
could not fetch de data. When using my Apache2 web server everything works fine.
I search but could not find how to set the port value in the http array for the context.
And that don't tell me why it was working for the command line and in a single script.
Upvotes: 1