Skip to content

Instantly share code, notes, and snippets.

@spin0us
Last active December 15, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spin0us/5244593 to your computer and use it in GitHub Desktop.
Save spin0us/5244593 to your computer and use it in GitHub Desktop.
function do_post_request($url, $data, $optional_headers = null) {
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$stream = @fopen($url, 'rb', false, $ctx);
if (!$stream) {
if(isset($php_errormsg) && preg_match("/401/", $php_errormsg)) header("HTTP/1.1 401 Authentication failed");
else header("HTTP/1.1 403 Forbidden");
die();
}
$response = @stream_get_contents($stream);
fclose($stream);
if ($response === false || empty($response)) {
throw new Exception("Problem reading data from $url");
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment