Posted By

ryansobol on 10/31/06


Tagged

get http php post socket request


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

mdavie
blakeb
skywalker


send_http_request()


 / Published in: PHP
 

Sends a request to a supplied path for a given host. It can send either a GET or POST request and can pass parameters.

  1. function send_http_request($host, $path, $args, $method)
  2. {
  3. if (empty($host) || empty($path) || empty($args) || empty($method)) return false;
  4.  
  5. $temp = array();
  6. foreach ($args as $key => $value) { array_push($temp, "$key=$value"); }
  7.  
  8. $params = implode("&", $temp);
  9. $params_length = strlen($params);
  10.  
  11. $output = <<<END
  12. $method $path HTTP/1.0
  13. Host: $host
  14. User-Agent: PHP-script
  15. Content-Type: application/x-www-form-urlencoded
  16. Content-Length: $params_length
  17.  
  18. $params
  19. END;
  20.  
  21. global $logger; $logger->debug($output);
  22.  
  23. if ($socket = fsockopen($host, 80))
  24. {
  25. $result = fwrite($socket, $output);
  26. fclose($socket);
  27.  
  28. return $result;
  29. }
  30.  
  31. return false;
  32. }

Report this snippet  

You need to login to post a comment.