HTTP POST from PHP, without cURL


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

From http://netevil.org/blog/2006/nov/http-post-from-php-without-curl


Copy this code and paste it in your HTML
  1. <?php
  2. function do_post_request($url, $data, $optional_headers = null)
  3. {
  4. $params = array('http' => array(
  5. 'method' => 'POST',
  6. 'content' => $data
  7. ));
  8. if ($optional_headers !== null) {
  9. $params['http']['header'] = $optional_headers;
  10. }
  11. $ctx = stream_context_create($params);
  12. $fp = @fopen($url, 'rb', false, $ctx);
  13. if (!$fp) {
  14. throw new Exception("Problem with $url, $php_errormsg");
  15. }
  16. $response = @stream_get_contents($fp);
  17. if ($response === false) {
  18. throw new Exception("Problem reading data from $url, $php_errormsg");
  19. }
  20. return $response;
  21. }

URL: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.