Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.