Posted By

g0mer on 04/28/09


Tagged

php textmate request asyncron


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

g0mer
jfherring
Dean_IconWeb


Asyncron HTTP request


 / Published in: PHP
 

  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  

You need to login to post a comment.