Skip to content

Instantly share code, notes, and snippets.

@Mark-Shine
Last active April 8, 2016 08:46
Show Gist options
  • Save Mark-Shine/11243497 to your computer and use it in GitHub Desktop.
Save Mark-Shine/11243497 to your computer and use it in GitHub Desktop.
Php 发送POST请求
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);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
$data = array(
"time"=>time(),);
$postdata = http_build_query($data);
$this->do_post_request("http://localhost:8080/test", $postdata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment