有些API要求请求的内容类型为application/x-www-form-urlencoded
- function requestUrl($url,$data=null,$https=true,$method='post'){
- //1.初始化url
- $ch = curl_init($url);
- //2.设置相关的参数
- //字符串不直接输出,进行一个变量的存储
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //判断是否为https请求
- if($https === true){
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- //判断是否为post请求
- if($method == 'post'){
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded'));
- //3.发送请求
- $str = curl_exec($ch);
- //4.关闭连接
- curl_close($ch);
- //6.返回请求到的结果
- return $str;
- }