Asyncron HTTP request


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function backgroundPost($url,$params) {
  2.  
  3. $post_string = ''; // fallback
  4.  
  5. if(!empty($params)) {
  6. foreach($params as $key => &$val) {
  7. if(is_array($val)) $val = implode(',', $val);
  8. $post_params[] = $key.'='.urlencode($val);
  9. }
  10. $post_string = implode('&', $post_params);
  11. }
  12. $parts=parse_url($url);
  13.  
  14. $fp = fsockopen($parts['host'],
  15. isset($parts['port'])?$parts['port']:80,
  16. $errno, $errstr, 30);
  17.  
  18. if(!$fp) {
  19. return false;
  20. } else {
  21. $out = "POST ".$parts['path']." HTTP/1.1rn";
  22. $out.= "Host: ".$parts['host']."rn";
  23. $out.= "Content-Type: application/x-www-form-urlencodedrn";
  24. $out.= "Content-Length: ".strlen($post_string)."rn";
  25. $out.= "Connection: Closernrn";
  26. if (isset($post_string)) $out.= $post_string;
  27.  
  28. fwrite($fp, $out);
  29. fclose($fp);
  30. return true;
  31. }
  32. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.