摘要:函数代码支持,烟火里的尘埃请求地址参数数据如果服务器返回则返回,不然则返回初始化设置传输选项方式获取采集结果关闭链接解析判断还是验证解析
概念描述
应用场景CURL是一个非常强大的开源库,支持很多协议,包括HTTP、FTP、TELNET等,可以使用cURL实现Get和Post请求的方法。
函数代码在开发中服务端调用API 时,经常用到向第三方API发起GET 或 POST 请求,然后得到返回结果,有可能是 json 或者 xml ,甚至 我们需要配置一些HTTP 协议 头信息一起发起请求。这时候就需要用到 cURL 。 比如、天气预报借口、微信接口、支付接口等。
/**
* [cUrl cURL(支持HTTP/HTTPS,GET/POST)]
* @author qiuguanyou
* @copyright 烟火里的尘埃
* @version V1.0
* @date 2017-04-12
* @param [string] $url [请求地址]
* @param [Array] $header [HTTP Request headers array("Content-Type"=>"application/x-www-form-urlencoded")]
* @param [Array] $data [参数数据 array("name"=>"value")]
* @return [type] [如果服务器返回xml则返回xml,不然则返回json]
*/
public static function cUrl($url,$header=null, $data = null){
//初始化curl
$curl = curl_init();
//设置cURL传输选项
if(is_array($header)){
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){//post方式
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
//获取采集结果
$output = curl_exec($curl);
//关闭cURL链接
curl_close($curl);
//解析json
$json=json_decode($output,true);
//判断json还是xml
if ($json) {
return $json;
}else{
#验证xml
libxml_disable_entity_loader(true);
#解析xml
$xml = simplexml_load_string($output, "SimpleXMLElement", LIBXML_NOCDATA);
return $xml;
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/21786.html
摘要:给大家分享一些博主自己写的函数临近下班了,大约还有分钟左右,手头没事,给大家分享几个函数。 给大家分享一些博主自己写的PHP函数 临近下班了,大约还有20分钟左右,手头没事,给大家分享几个函数。超级好用哟! 截取字符串函数 /** * @param string $begin 开始字符串 * @param string $end 结束字符串 * @param st...
摘要:给大家分享一些博主自己写的函数临近下班了,大约还有分钟左右,手头没事,给大家分享几个函数。 给大家分享一些博主自己写的PHP函数 临近下班了,大约还有20分钟左右,手头没事,给大家分享几个函数。超级好用哟! 截取字符串函数 /** * @param string $begin 开始字符串 * @param string $end 结束字符串 * @param st...
摘要:给大家分享一些博主自己写的函数临近下班了,大约还有分钟左右,手头没事,给大家分享几个函数。 给大家分享一些博主自己写的PHP函数 临近下班了,大约还有20分钟左右,手头没事,给大家分享几个函数。超级好用哟! 截取字符串函数 /** * @param string $begin 开始字符串 * @param string $end 结束字符串 * @param st...
阅读 1770·2021-09-04 16:40
阅读 3673·2021-07-28 00:13
阅读 3058·2019-08-30 11:19
阅读 2831·2019-08-29 12:29
阅读 3347·2019-08-29 12:24
阅读 1278·2019-08-26 13:28
阅读 2619·2019-08-26 12:01
阅读 3700·2019-08-26 11:35