0

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 &quot;http&quot; - did you forget to enable it when you configured PHP?

Same with or without 'request_fulluri' => true, or 'request_fulluri' => false,

ricko zoe
  • 496
  • 1
  • 6
  • 19
  • have you try to remove \ before `file_get_contents` method ? – vardius Feb 03 '16 at 11:15
  • yes of course, it does not do anything more or less (And I'm surprised because I was thinking if we in a specific namespace, we need to specified with the \ that we want to use the PHP standard library function specefied after the \. For exemple when I use ZipArchive class without the \, I got a error of context. – ricko zoe Feb 03 '16 at 11:20
  • @Vardius what do you think about proxy? 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? – ricko zoe Feb 03 '16 at 11:25
  • \ is for including namespaces and classes, not for using a global php functions – vardius Feb 03 '16 at 12:37
  • And about proxy follow this answer it should help you http://stackoverflow.com/a/1336419/2160958 `$aContext = array( 'http' => array( 'proxy' => 'tcp://192.168.0.2:3128', 'request_fulluri' => true, 'header' => "Proxy-Authorization: Basic $auth", ), ); $cxContext = stream_context_create($aContext);` – vardius Feb 03 '16 at 12:40
  • Thanks but l already find it, only difference is Proxy-Authorization, I got zero credentials for the proxy I just put some export on my .zshrc file for programs like Composer and it works just fine. It don't tell my why my code works in command line but not in the Symfony Controller. – ricko zoe Feb 03 '16 at 12:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102479/discussion-between-vardius-and-ricko-zoe). – vardius Feb 03 '16 at 13:15

1 Answers1

1

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.

ricko zoe
  • 496
  • 1
  • 6
  • 19