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?
Nobody seems to mention Guzzle, which is a PHP HTTP client that makes it easy to send HTTP requests. It can work with or without
Curl
. It can send both synchronous and asynchronous requests.For PHP5.5+, mpyw/co is the ultimate solution. It works as if it is tj/co in JavaScript.
Example
Assume that you want to download specified multiple GitHub users' avatars. The following steps are required for each user.
<img class="avatar" src="...">
and request it (GET IMAGE)---
: Waiting my response...
: Waiting other response in parallel flowsMany famous
curl_multi
based scripts already provide us the following flows.However, this is not efficient enough. Do you want to reduce worthless waiting times
...
?Yes, it's very easy with mpyw/co. For more details, visit the repository page.
If you are using Linux environment then you can use the PHP's exec command to invoke the linux curl. Here is a sample code, which will make a Asynchronous HTTP post.
This code does not need any extra PHP libs and it can complete the http post in less than 10 milliseconds.
Based on this thread I made this for my codeigniter project. It works just fine. You can have any function processed in the background.
A controller that accepts the async calls.
And a library that does the async calls
This method can be called to process any model::method() in the 'background'. It uses variable arguments.