I wish to make a simple GET request to another script on a different server. How do I do this?
In one case, I just need to request an external script without the need for any output.
make_request('http://www.externalsite.com/script1.php?variable=45'); //example usage
In the second case, I need to get the text output.
$output = make_request('http://www.externalsite.com/script2.php?variable=45');
echo $output; //string output
To be honest, I do not want to mess around with CURL as this isn't really the job of CURL. I also do not want to make use of http_get as I do not have the PECL extensions.
Would fsockopen work? If so, how do I do this without reading in the contents of the file? Is there no other way?
Thanks all
Update
I should of added, in the first case, I do not want to wait for the script to return anything. As I understand file_get_contents() will wait for the page to load fully etc?
Regarding your update, about not wanting to wait for the full page to load - I think a HTTP
HEAD
request is what you're looking for..get_headers should do this - I think it only requests the headers, so will not be sent the full page content.
"PHP / Curl: HEAD Request takes a long time on some sites" describes how to do a
HEAD
request using PHP/CurlIf you want to trigger the request, and not hold up the script at all, there are a few ways, of varying complexities..
"wget -O /dev/null $carefully_escaped_url"
- this will be platform specific, and you have to be really careful about escaping parameters to the commandfile_get_contents
will do what you wantEdit: One way to fire off a GET request and return immediately.
Quoted from http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html
What this does is open a socket, fire off a get request, and immediately close the socket and return.
This works fine for me, sadly you cannot retrieve the response from your request:
It works very fast, no need for raw tcp sockets :)
You'd better consider using Message Queues instead of advised methods. I'm sure this will be better solution, although it requires a little more job than just sending a request.
Try:
This will NOT work as an apache module, you need to be using CGI.
Interesting problem. I'm guessing you just want to trigger some process or action on the other server, but don't care what the results are and want your script to continue. There is probably something in cURL that can make this happen, but you may want to consider using
exec()
to run another script on the server that does the call if cURL can't do it. (Typically people want the results of the script call so I'm not sure if PHP has the ability to just trigger the process.) Withexec()
you could run awget
or even another PHP script that makes the request withfile_get_conents()
.