Last active
April 8, 2016 08:46
-
-
Save Mark-Shine/11243497 to your computer and use it in GitHub Desktop.
Php 发送POST请求
This file contains hidden or 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); | |
$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