views:

31

answers:

3

I have to run a get request from a PHP script, but I'm in a fairly limited environment (tight firewalls, can't modify the PHP install, etc...).

fsockopen() returns an error and http_get() seems to not be installed. The only other way I know of is to call file_get_contents(), which works OK, except for the fact that I need to set the referrer header in the request (which I don't think you can do with file_get_contents().

Does anyone know any other way to run a GET request via php?

Thanks!

edit: also, CURL is unavailable. :-(

+1  A: 

See CURL functions in PHP.

m1tk4
Thanks for the answer - I forgot to add CURL to the list of methods which aren't available. :-( Thanks, though!
loneboat
A: 

After some additional research, it looks like my best bet given the resources I have is to use stream_context_create() to create the headers in the context stream, which you can apparently sent to file_get_contents(). Didn't know that!

loneboat
+2  A: 

You can set the a header using file_get_contents() with an HTTP context. For example

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Referer: http://foo.bar.com\r\n"
  )
);

$context = stream_context_create($opts);
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
Otterfan
Thanks Otterfan! I ran across that answer and posted it here about 8 minutes before you :-D - Thanks for your help!
loneboat
+1 for beautiful format.
JClaspill