Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an asynchronous GET request in PHP?

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?

like image 359
Abs Avatar asked Jun 07 '09 21:06

Abs


People also ask

How do you send asynchronous request?

You can also send requests synchronously by calling WdfRequestSend, but you have to format the request first by following the rules that are described in Sending I/O Requests Asynchronously. Sending I/O requests to an I/O target synchronously is simpler to program than sending I/O requests asynchronously.

Which method is used to make an asynchronous HTTP request?

Ajax. Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method.

Does PHP support asynchronous?

PHP has no built in support for asynchronous calls. You can make pseudo-asynchronous calls using curl.

What is an asynchronous HTTP request?

Asynchronous HTTP Request Processing is a relatively new technique that allows you to process a single HTTP request using non-blocking I/O and, if desired in separate threads. Some refer to it as COMET capabilities.


2 Answers

file_get_contents will do what you want

$output = file_get_contents('http://www.example.com/'); echo $output; 

Edit: 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

function curl_post_async($url, $params) {     foreach ($params as $key => &$val) {       if (is_array($val)) $val = implode(',', $val);         $post_params[] = $key.'='.urlencode($val);     }     $post_string = implode('&', $post_params);      $parts=parse_url($url);      $fp = fsockopen($parts['host'],         isset($parts['port'])?$parts['port']:80,         $errno, $errstr, 30);      $out = "POST ".$parts['path']." HTTP/1.1\r\n";     $out.= "Host: ".$parts['host']."\r\n";     $out.= "Content-Type: application/x-www-form-urlencoded\r\n";     $out.= "Content-Length: ".strlen($post_string)."\r\n";     $out.= "Connection: Close\r\n\r\n";     if (isset($post_string)) $out.= $post_string;      fwrite($fp, $out);     fclose($fp); } 

What this does is open a socket, fire off a get request, and immediately close the socket and return.

like image 116
Marquis Wang Avatar answered Sep 16 '22 16:09

Marquis Wang


This is how to make Marquis' answer work with both POST and GET requests:

  // $type must equal 'GET' or 'POST'   function curl_request_async($url, $params, $type='POST')   {       foreach ($params as $key => &$val) {         if (is_array($val)) $val = implode(',', $val);         $post_params[] = $key.'='.urlencode($val);       }       $post_string = implode('&', $post_params);        $parts=parse_url($url);        $fp = fsockopen($parts['host'],           isset($parts['port'])?$parts['port']:80,           $errno, $errstr, 30);        // Data goes in the path for a GET request       if('GET' == $type) $parts['path'] .= '?'.$post_string;        $out = "$type ".$parts['path']." HTTP/1.1\r\n";       $out.= "Host: ".$parts['host']."\r\n";       $out.= "Content-Type: application/x-www-form-urlencoded\r\n";       $out.= "Content-Length: ".strlen($post_string)."\r\n";       $out.= "Connection: Close\r\n\r\n";       // Data goes in the request body for a POST request       if ('POST' == $type && isset($post_string)) $out.= $post_string;        fwrite($fp, $out);       fclose($fp);   } 
like image 29
catgofire Avatar answered Sep 17 '22 16:09

catgofire