Advertisement
cgrunwald

Untitled

Sep 23rd, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1.  
  2.     /**
  3.      * Does an asynchronous POST request. (Cannot access output)
  4.      *
  5.      * @static
  6.      * @throws Exception
  7.      * @param string $url
  8.      * @param array $params
  9.      * @return void
  10.      */
  11.     public static function curl_post_async($url, $params)
  12.     {
  13.         $post_params = array();
  14.         foreach($params as $key => &$val) {
  15.             if(is_array($val)) $val = implode(',', $val);
  16.             $post_params[] = $key . '=' . urlencode($val);
  17.         }
  18.  
  19.         $post_string = implode('&', $post_params);
  20.         $parts = parse_url($url);
  21.  
  22.         $fp = fsockopen('127.0.0.1',
  23.             isset($parts['port']) ? $parts['port'] : 80,
  24.             $errno, $errstr, 30);
  25.  
  26.         if($fp == 0) throw new Exception("Couldn't open a socket to " . $url . " (" . $errstr . ")");
  27.  
  28.         $out = "POST " . $parts['path'] . " HTTP/1.1\r\n";
  29.         $out .= "Host: " . $parts['host'] . "\r\n";
  30.         $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  31.         $out .= "Content-Length: " . strlen($post_string) . "\r\n";
  32.         $out .= "Connection: Close\r\n\r\n";
  33.         if(isset($post_string)) $out .= $post_string;
  34.  
  35.         fwrite($fp, $out);
  36.         fclose($fp);
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement