开发者

GET request from a PHP script in a limited environment

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 开发者_Python百科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. :-(


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);


See CURL functions in PHP.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜