Posted By

halk on 11/07/12


Tagged

php post function


Versions (?)

POST Function Using HTTP Headers Return Result In A Variable


 / Published in: PHP
 

This function dosen't use curl, or any other libraries. Just include it in your function library and use it like this: $Postresult = _post('http://www.yourdomain.com/yourpath/post_api.php',"method=fetchJson&params=select * from user");

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

Report this snippet  

You need to login to post a comment.