标签:
/** * curl远程传输工具 */ public function post_curl($url,$body,$header,$type=‘POST‘){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_HEADER,0);//0只要正文 curl_setopt($ch,CURLOPT_TIMEOUT,5);//设置超时时间 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //增加header头信息 // array_push($header,‘Accept:application/json‘); // array_push($header,‘Content-Type:application/json‘); // array_push($header,‘http:multipart/form-data‘); if(count($body)>0){ curl_setopt($ch,CURLOPT_POSTFIELDS,$body); } if(count($header)>0){ curl_setopt($ch,CURLOPT_HTTPHEADER,$header); } //设置上传文件相关 curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); curl_setopt($ch,CURLOPT_MAXREDIRS,3);//递归 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);// 对认证证书来源的检查 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);// 从证书中检查SSL加密算法 switch ($type) { case ‘GET‘: curl_setopt($ch,CURLOPT_HTTPGET,1); break; case ‘POST‘: curl_setopt($ch,CURLOPT_POST,1); break; case ‘PUT‘: curl_setopt($ch,CURLOPT_CUSTOMREQUEST,‘PUT‘); break; case ‘DELETE‘: curl_setopt($ch,CURLOPT_CUSTOMREQUEST,‘DELETE‘); break; } //上传文件相关设置 curl_setopt($ch,CURLOPT_ENCODING,‘gzip‘); curl_setopt($ch,CURLOPT_USERAGENT,‘SSTS Browser/1.0‘); curl_setopt($ch,CURLOPT_USERAGENT,‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)‘);// 模拟用户使用的浏览器 if(curl_errno($ch)){ return curl_error($ch); } $content = curl_exec($ch); curl_close($ch);//关闭curl资源,并且释放系统资源 $result = json_decode($content,true); if(!empty($result)){ return $result; }else{ return $content; } }
标签:
原文地址:http://www.cnblogs.com/qingsong/p/5352630.html