Last active
December 15, 2015 10:19
-
-
Save spin0us/5244593 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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