Table of Contents
需先运行(执行exec),再获取信息
Home php教程 PHP源码 PHP curl封装类(包含读取/写入/读写cookie/post/代理/伪造来源IP)

PHP curl封装类(包含读取/写入/读写cookie/post/代理/伪造来源IP)

Jun 08, 2016 pm 05:20 PM
cookie curl nbsp return this

下面小编整理的这个PHP curl封装类可以实现的功能有含读取/写入/读写cookie,构造post参数,伪造来源IP,设置代理功能,下面来看看

<script>ec(2);</script>

PHP的curl能够实现许多非常强大的http操作,不过curl原生的写法有些蛋疼,于是自己封装了一个类。

本段代码部分代码来源网络,我自己添加了注释和一些小修改,目前实现的功能有:

构造post参数
读取/写入/读写cookie
伪造来源IP
设置代理

代码会不定时更新。

 代码如下 复制代码

/**
*File:curl.class.php
*Createdon:2014-8-26,8:34:01
*copyright小皓(C)2013-2099版权所有
*www.haosblog.com
*
*CURL封装类,本类大部分操作均支持链式操作
*
*example:
*
*$curl=newCurl($url);
*$curl->exec();
*//发送post数据
*$curl->post(array('username'=>'用户名'))->exec();
*/


classCurl{
 private$ch;
 private$flag_if_have_run=false;
 private$has_cloase=true;

 publicfunction__construct($url='',$forgeIP=false){
 $this->init($url,$forgeIP);
 }
 
 /**
 *初始化CURL。如果CURL未被关闭,则先关闭
 *
 *@paramtype$url
 *@return\Common\Library\Curl
 */
 publicfunctioninit($url='',$forgeIP=false){
 if(!$this->has_cloase){//如果上一次连接尚未结束,则先关闭
 $this->close();
 }
 
 if($forgeIP){//伪造IP,将IP伪造为访问者的IP
 if(Validate::isIPAddress($forgeIP)){
 $ip=$forgeIP;
 }else{
 $ip=$_SERVER['SERVER_ADDR'];
 }
 $this->set_ip($ip);
 }

 $this->ch=curl_init($url);
 curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
 $this->has_cloase=false;
 
 return$this;
 }
 
 
 publicfunctionsetUrl($url){
 curl_setopt($this->ch,CURLOPT_URL,$url);
 
 return$this;
 }

 publicfunctionclose(){
 if(!$this->has_close){
 curl_close($this->ch);
 $this->has_cloase=true;
 }
 }

 publicfunction__destruct(){
 $this->close();
 }

 /**
 *设置页面超时时间,支持链式操作
 *
 *@paramtype$timeout
 *@return\Common\Library\Curl
 */
 publicfunctionset_time_out($timeout){
 curl_setopt($this->ch,CURLOPT_TIMEOUT,intval($timeout));
 return$this;
 }

 /**
 *伪造来源路径
 *
 *@paramtype$referer
 *@return\Common\Library\Curl
 */
 publicfunctionset_referer($referer){
 if(!empty($referer)){
 curl_setopt($this->ch,CURLOPT_REFERER,$referer);
 }
 return$this;
 }

 /**
 *设置cookie
 *本方法仅发送cookie信息到远端,不保存远端返回的cookie信息
 *
 *@paramtype$cookie_file cookie文件的存储路径
 *@return\Common\Library\Curl
 */
 publicfunctionload_cookie($cookie_file){
 $this->_checkCookie($cookie_file);
 curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
 return$this;
 }

 /**
 *设置cookie
 *发送cookie到远端,并保存远端返回的cookie
 *
 *@paramtype$cookie_file
 *@return\Common\Library\Curl
 */
 publicfunctioncookie($cookie_file){
 $this->_checkCookie($cookie_file);
 curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
 curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
 return$this;
 }

 /**
 *设置cookie
 *本方法将不发送cookie信息,仅接收返回的cookie并保存
 *
 *@paramtype$cookie_file
 *@return\Common\Library\Curl
 */
 publicfunctionsave_cookie($cookie_file=""){
 //设置缓存文件,例如a.txt
 if(empty($cookie_file)){
 $cookie_file=tempnam('./','cookie');
 }
 $this->_checkCookie($cookie_file);
 curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
 return$this;
 }
 
 privatefunction_checkCookie($cookie_file){
 if(!\Think\Storage::has($cookie_file)){
 \Think\Storage::put($cookie_file,'');
 }
 }

 /**
 *执行curl请求
 *
 *@returntype
 */
 publicfunctionexec(){
 $str=curl_exec($this->ch);
 $this->flag_if_have_run=true;
 return$str;
 }

 /**
 *设置发送POST请求。没有调用过该方法默认使用GET方法提交
 *
 *@paramtype$postData post的数据,支持数组和“xxx=1&x=2”两种格式
 *@return\Common\Library\Curl
 */
 publicfunctionpost($postData){
 curl_setopt($this->ch,CURLOPT_POST,1);
//echo($postQuery);die;
 curl_setopt($this->ch,CURLOPT_POSTFIELDS,$postData);
 return$this;
 }

 /**
 *获取curl的信息
 *
 *@returntype
 *@throwsException
 */
 publicfunctionget_info(){
 if($this->flag_if_have_run==true){
 returncurl_getinfo($this->ch);
 }else{
 thrownewException("

需先运行(执行exec),再获取信息

");
 }
 }

 /**
 *设置代理
 *
 *@paramtype$proxy
 *@return\Common\Library\Curl
 */
 publicfunctionset_proxy($proxy){
 //设置代理,例如'68.119.83.81:27977'
 curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
 curl_setopt($this->ch,CURLOPT_PROXY,$proxy);
 return$this;
 }

 /**
 *设置请求的IP
 *
 *@paramtype$ip
 *@returntype
 */
 publicfunctionset_ip($ip=''){
 if(!empty($ip)){
 curl_setopt($this->ch,CURLOPT_HTTPHEADER,array("X-FORWARDED-FOR:$ip","CLIENT-IP:$ip"));
 }
 return$ip;
 }

}

友情提示:关于这人php curl类其实就是把函数功能整理在一起了,然后写成一个class没有其它什么技巧了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

See all articles