POST Function Using HTTP Headers Return Result In A Variable


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

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.