curl http请求接口方式
function http_get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$SSL = substr($url, 0, 8) == "https://" ? true : false;
if ($SSL) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
function post($url, $params = [])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:1314/api/' . $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$data = curl_exec($curl);
curl_close($curl);
return json_decode($data, true);
}