搜索
系统检测到您的用户名不符合规范:

Http请求类

浏览:3029 发布日期:2017年12月15日 分类:功能实现 关键字: curl http请求
自己常用的一个http请求类,和常用的区别是假如对方返回数据有utf8的bom头会自动去掉,防止解析数据出错
经常对接接口的同学可能碰到过一个问题:就是明明看着对方返回的数据是对的,可json_decode解码总是出错。那其中一个原因可能就是:对方返回的数据是utf8的并且带着 BOM头!!!
此代码特别的地方就是,假如有bom头会自动去掉。
小牛刀代码后期会长发,可加群共同学习QQ群 616405640class HttpSvc
{
const TIME_OUT=3;
static $ALL_CURL=array('post','get');
public static function httpReqUrl($url,$method='get',$timeout=1)
{
if(!in_array(strtolower($method),self::$ALL_CURL))
return $url;
if(!$url)
return ;
$param = array();
$urlarr = explode("?",$url,2);
$url = $urlarr[0];
if(isset($urlarr[1]) && !empty($urlarr[1])) {
parse_str($urlarr[1],$param);
}
$res=self::HttpReqArr($url,$param,$timeout,strtolower($method));
return $res['data'];
}

public static function HttpReqArr($url,$opts=array(),$timeout=3,$mothod='post',$proxy=null)
{
$data=array();
// $logger = Logger::ins();
try{
$stime=microtime(true);
$url = trim($url);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

if($proxy)
curl_setopt($ch,CURLOPT_PROXY,$proxy);
if(isset($opts) && count($opts)>0)
{
$pstring='';
foreach($opts as $key => $val)
$pstring .= trim($key) . '=' . urlencode(trim($val)) . "&";
$pstring = substr($pstring,0,-1);

if(strtolower($mothod)=='post') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pstring);
curl_setopt($ch, CURLOPT_URL, $url);
}
else
{
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, $url."?".$pstring);
}
}
else
{
if(strtolower($mothod)=='post')
curl_setopt($ch, CURLOPT_POST, true);
else
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, $url);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$r = curl_exec($ch);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$etime=microtime(true);
$usetime=$etime-$stime;
// $logger->debug("[response] code: $http_code, usetime: $usetime, url:$url");
$error = curl_error($ch);
if($error){
// $logger->error("code: $http_code, usetime: $usetime, callbackUrl:$url?$pstring method:$mothod opt=".var_export($opts,true).'&proxy='.$proxy);
}
}catch(Exception $e) {
// $logger->error("code: $http_code, usetime: $usetime, callbackUrl:$url?$pstring method:$mothod opt=".var_export($opts,true).' exception '.$e->getMessage()."&proxy=$proxy");
}
curl_close($ch);
$r = self::RemoveBom($r);
$data=array('code'=>$http_code,'data'=>$r);
// $logger->info("code: $http_code, usetime: $usetime, method:$mothod,callBackUrl:$url?$pstring ,putData:".$r.",proxy=$proxy");
return $data;
}
public static function RemoveBom($content)
{
if(substr($content, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
$content=substr($content, 3);
}
return $content;
}
}
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
304455977
积分:725 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /