0

Call me out if this is a duplicate question, but this is starting to get ridiculous. I want to, with PHP:

 GET http://www.example.com/hello.xyz

And add this header to the request:

 "X-Header-Name: $foobar" 

where foobar comes from a php variable that is already defined.

and then store the response in a variable. That's it! Nothing more, nothing less. But I can't find it! I don't want to use curl or anything like that, it'd slow it down too much if I run curl everytime. Edit: My main concern with using curl is about compatibility with Windows (local server) vs. Linux (deployment server).

<?php

echo "So, how do I do it in the simplest way possible?";

?>
Austin Burk
  • 930
  • 4
  • 15
  • 33
  • 4
    A library like curl is the simplest way possible. *Any* library you use is likely to "slow down" script execution, as it's the request itself that takes the time. Googling `How do I send a GET request with a header from PHP?` gets 2 good results that show how – Pekka Oct 08 '13 at 22:20
  • If you don't want to use curl for performance reasons, you have to implement and measure it in curl. Where is your code that is too slow? – Sven Oct 08 '13 at 22:21
  • 1
    Why not use cURL? It will also give you a lot more functionality for handling errors and such. Also, if you are concerned about making a large number of requests in a short timeframe, you can execute parallel requests using `curl_multi_exec()`, which should allow your script to execute much more quickly then doing a bunch of serial `shell_exec()` commands or whatever. I have a nice lightweight `curl_multi_exec()` REST client at https://github.com/mikecbrant/php-rest-client you can feel free to use. – Mike Brant Oct 08 '13 at 22:21
  • You can use the socket functions in PHP. http://php.net/manual/en/book.sockets.php. You just have to do all the things the cURL lib does, manually. – asafreedman Oct 08 '13 at 22:26
  • Oh, I thought that I'd be running a lot of subprocesses from PHP. A concern of mine is that I'll have to deal with compatibility issues if/when I switch to Windows for local testing. – Austin Burk Oct 08 '13 at 23:24

1 Answers1

3

You may use file_get_contents if you don't want to use curl but not sure about speed but it's php's built in function where curl is not. When talking about speed then I think whatever you use for a remote request, the speed/performance will depend on the network connection speed more than the function/library and maybe there is a bit different among these (curl/file_get_contents/fsockopen) but I think it'll be a very little (1-2 %) and you can't catch the difference, it'll seem almost same.

$opts = array(
  'http'=>array(
      'method'=>"GET",
      'header'=>"X-Header-Name: $foobar"
));
$context = stream_context_create($opts);
$data = file_get_contents('http://www.example.com/hello.xyz', false, $context);
if($data) {
    // do something with data
}

Also, if you want to use curl then you may use this

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Header-Name: $foobar"));
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/hello.xyz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($curl_errno == 0) {
    // $data received in $data
}

Also, check this answer, it may help you to decide.

The Alpha
  • 143,660
  • 29
  • 287
  • 307