$timeout, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $header, CURLOPT_URL => count($params) > 0 ? $url . '?' . http_build_query($params) : $url, ); $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if ($error) throw new \Exception('http get error:' . $error); return $data; } /** * 模拟http post请求 * @param $url * @param $params * @param array $header * @param int $timeout * @return bool|string * @throws \Exception */ public static function post($url, $params, $header = array(), $timeout=30){ $opts = array( CURLOPT_TIMEOUT => $timeout, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $header, CURLOPT_URL => $url, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => is_array($params) ? http_build_query($params) : $params ); $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if ($error) throw new \Exception('http post error:' . $error); return $data; } /** * 模拟http post json请求 * @param string $url * @param array $params * @param array $header * @param int $timeout * @return bool|string * @throws \Exception */ public static function postJson(string $url, array $params, array $header = [], int $timeout=30){ $opts = [ CURLOPT_TIMEOUT => $timeout, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => array_merge(["content-type:application/json"],$header), CURLOPT_URL => $url, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => json_encode($params) ]; $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if ($error) throw new \Exception('http post error:' . $error); return json_decode($data,true); } }