Home > php_mysql > Asynchronous HTTP request in PHP

Asynchronous HTTP request in PHP

PHP:
  1. function curl_post_async($url, $params)
  2. {
  3.     foreach ($params as $key => &$val) {
  4.       if (is_array($val)) $val = implode(',', $val);
  5.         $post_params[] = $key.'='.urlencode($val);
  6.     }
  7.     $post_string = implode('&', $post_params);
  8.  
  9.     $parts=parse_url($url);
  10.  
  11.     $fp = fsockopen($parts['host'],
  12.         isset($parts['port'])?$parts['port']:80,
  13.         $errno, $errstr, 30);
  14.  
  15.     pete_assert(($fp!=0), "Couldn't open a socket to ".$url." (".$errstr.")");
  16.  
  17.     $out = "POST ".$parts['path']." HTTP/1.1\r\n";
  18.     $out.= "Host: ".$parts['host']."\r\n";
  19.     $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
  20.     $out.= "Content-Length: ".strlen($post_string)."\r\n";
  21.     $out.= "Connection: Close\r\n\r\n";
  22.     if (isset($post_string)) $out.= $post_string;
  23.  
  24.     fwrite($fp, $out);
  25.     fclose($fp);
  26. }

Categories: php_mysql Tags:
  1. No comments yet.
  1. No trackbacks yet.

0 pages viewed, 0 today
0 visits, 0 today
FireStats icon Powered by FireStats