curl类封装
php
[PHP]代码
<?php /** * @author askwei **/ class CURL { private $ch; private $url = "http://www.baidu.com"; private $flag_if_have_run; //标记exec是否已经运行 private $set_time_out = 20; //设置curl超时时间 private $cookie_file = ""; //cookie_file路径 private $cookie_mode = 0; //cookie保存模式 0不使用 1客户端、2服务器文件 private $show_header = 0; //是否输出返回头信息 private $set_useragent = ""; //模拟用户使用的浏览器,默认为模拟 //构造函数 public function __construct($url = ""){ $this->ch = curl_init(); $this->url = $url ? $url : $this->url; //$this->set_useragent = $_SERVER['HTTP_USER_AGENT']; // 模拟用户使用的浏览器 $this->set_useragent ="Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/7.0 Mobile/10B350 Safari/9537.53"; // $this->set_useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"; //$this->cookie_file=dirname(__FILE__)."/cookie_".md5(basename(__FILE__)).".txt"; //初始化cookie文件路径 //$this->cookie_file= SAE_TMP_PATH.TmpFS; $this->cookie_file = "saekv://cookie_2014.txt"; } //关闭curl public function close(){ curl_close($this->ch); } //析构函数 public function __destruct(){ $this->close(); } //设置超时 public function set_time_out($timeout=20){ if(intval($timeout) != 0) $this->set_time_out = $timeout; return $this; } //设置来源页面 public function set_referer($referer = ""){ if (!empty($referer)) curl_setopt($this->ch, CURLOPT_REFERER , $referer); return $this; } //设置cookie存放模式 1客户端、2服务器文件 public function set_cookie_mode($mode = ""){ $this->cookie_mode = $mode; return $this; } //载入cookie public function load_cookie(){ if($this->cookie_mode == 1 ) { if(isset($_COOKIE['curl'])){ curl_setopt($this->ch,CURLOPT_COOKIE,$_COOKIE['curl']); }else{ $this->exec(); curl_setopt($this->ch,CURLOPT_COOKIE,$this->cookie_file); } } if($this->cookie_mode == 2 ) { curl_setopt($this->ch, CURLOPT_COOKIEFILE , $this->cookie_file); } if($this->cookie_mode == 3 ) { $kv = new SaeKV(); $ret = $kv->init(); $ret = $kv->get('curl_cookie'); if($ret) curl_setopt($this->ch,CURLOPT_COOKIE, $ret); } return $this; } //设置保存cookie方式 $cookie_val 模式1为变量 模式2为文件路径 public function save_cookie($cookie_val = "") { //保存在客户端 if($this->cookie_mode == 1 && $cookie_val){ setcookie('curl',$cookie_val); } //保存服务器端 if($this->cookie_mode == 2){ if(!empty($cookie_val)) $this->cookie_file = $cookie_val; curl_setopt($this->ch, CURLOPT_COOKIEJAR , $this->cookie_file); } //保存在sae if($this->cookie_mode == 3 && $cookie_val){ $kv = new SaeKV(); $ret = $kv->init(); $ret = $kv->get('curl_cookie'); if($ret){ $ret = $kv->set('curl_cookie', $cookie_val ); }else{ $ret = $kv->add('curl_cookie', $cookie_val); } } return $this; } //post参数 (array) $post public function post ($post = ""){ if($post && is_array($post)){ curl_setopt($this->ch, CURLOPT_POST , 1); curl_setopt($this->ch, CURLOPT_POSTFIELDS , $post ); } return $this; } //设置代理 ,例如'68.119.83.81:27977' public function set_proxy($proxy = ""){ if($proxy){ curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); curl_setopt($this->ch, CURLOPT_PROXY,$proxy); } return $this; } //设置伪造ip public function set_ip($ip=""){ if(!empty($ip)) curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("X-FORWARDED-FOR:$ip", "CLIENT-IP:$ip")); return $ip; } //设置是否显示返回头信息 public function show_header($show=0){ $this->show_header = 0; if($show) $this->show_header = 1; return $this; } //设置请求头信息 public function set_useragent($str=""){ if($str) $this->set_useragent = $str; return $this; } //执行 public function exec ($url = ""){ if(!$url) $url = $this->url; curl_setopt($this->ch, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER , 1 ); //获取的信息以文件流的形式返回 curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在 curl_setopt($this->ch, CURLOPT_USERAGENT, $this->set_useragent); // 模拟用户使用的浏览器 curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($this->ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->set_time_out); //超时设置 curl_setopt($this->ch, CURLOPT_HEADER, $this->show_header); // 显示返回的Header区域内容 curl_setopt($this->ch, CURLOPT_NOBODY, 0);//不返回response body内容 $res = curl_exec($this->ch); $this->flag_if_have_run = true; if (curl_errno($this->ch)) { //echo 'Errno'.curl_error($this->ch); return false; } if($this->show_header == 1){ //数组形式返回头信息和body信息 list($header, $body) = explode("\r\n\r\n", $res); $arr['header'] = $header; $arr['body'] = $body; if($this->cookie_mode == 1 || $this->cookie_mode == 3){ preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches); //print_r($matches); if($matches && isset($matches[1]) ){ $val = implode(';',array_unique(explode(';',implode(';',$matches[1])))); //去重处理 if($val) $this->save_cookie($val); //设置客户端保存cookie } } if($arr) return $arr; } return $res; } //返回 curl_getinfo信息 public function get_info(){ if($this->flag_if_have_run == true ) return curl_getinfo($this->ch); else throw new Exception("<h1>需先运行( 执行exec ),再获取信息</h1>"); } } ?>
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
4 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
R.E.P.O. 파일 저장 위치 : 어디에 있고 그것을 보호하는 방법은 무엇입니까?
3 몇 주 전
By DDD

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7316
9


자바 튜토리얼
1625
14


Cakephp 튜토리얼
1349
46


라라벨 튜토리얼
1261
25


PHP 튜토리얼
1208
29



이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는
