Posted By

jfoucher on 11/18/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jlan
dinkan


HTTP POST from PHP, without cURL


 / Published in: PHP
 

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

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

  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. }

Report this snippet  

You need to login to post a comment.