php调用接口

$result = Utils::httpRequest($url, $data, $type)

$url:请求的接口地址

$data:请求参数

$type:请求方式 0 - GET, 1 - POST


class Utils {

public static function httpRequest($url, $data, 0){

try{
$ch = curl_init();

if($method==0){
if(!empty($data)){
$url = $url.'?'.$data;
}
}elseif ($method==1){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if(strstr($url,'https://')){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}

//设置超时时间6s
//考虑到http请求会超时:服务器网络断开
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$curl_result = curl_exec($ch);

curl_close($ch);
}catch(Exception $e){
//Log::write("public:get_respond_by_url() exception error:".$e->getMessage(), "log");
return false;
}
return $curl_result;

}

}


你可能感兴趣的:(php调用接口)