/*
* 发起POST网络提交
* @params string $url : 网络地址
* @params json $data : 发送的json格式数据
*/
static public function https_post($url, $data) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/*
* 发起GET网络提交
* @params string $url : 网络地址
*/
static public function https_get($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
if (curl_errno($curl)) {
return 'Errno' . curl_error($curl);
} else {
$result = curl_exec($curl);
}
curl_close($curl);
return $result;
}
/**
* 异步post请求
* @param type $host 域名 www.baidu.com
* @param type $url 请求方法链接:/party.php/api/####
* @param type $param 参数数组
*/
static public function http_post_asynchronous_request($host = '', $url = '', $param = array()) {
$host = isset($host) ? $host : 'www.baidu.com';
$query = isset($param) ? http_build_query($param) : '';
$port = 80;
$errno = 0;
$errstr = '';
$timeout = 10;
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
$out = "POST " . $url . " HTTP/1.1\r\n";
$out .= "host:" . $host . "\r\n";
$out .= "content-length:" . strlen($query) . "\r\n";
$out .= "content-type:application/x-www-form-urlencoded\r\n";
$out .= "connection:close\r\n";
$out .= $query;
fputs($fp, $out);
usleep(20000); //避免nginx服务器链接断开无法 报499 链接失效
fclose($fp);
}