PHP封装CURL扩展类实例
作者:PHP开发学习门户 发布时间:2023-07-17 15:19:50
标签:PHP,CURL,扩展
本文实例讲述了PHP封装CURL扩展类。分享给大家供大家参考。具体如下:
<?php
/**
* @description: 封装CURL扩展
* @date: 2014-07-28 16:04
*/
/**
* @编码规范
* @class 类名首字母大写,类名为多个单词, 每个大字首字母大写 eg: class Curl , class CurlPage
* @variable 变量名小写, 变量名为多个单词, 每个单词小写,使用下划线_分割 eg: $curl_result
* @function 函数名与类名规则相同 eg: function SendRequest
* @params 函数形参规则与变量名相同
* @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $host_name_
*/
/**
* @要求
*
*/
class Curl{
/**
* @请求的host
*/
private $host_;
/**
* @curl 句柄
*/
private $ch_;
/**
* @超时限制时间
*/
const time_=5;
/**
* @请求的设置
*/
private $options_;
/**
* @保存请求头信息
*/
private $request_header_;
/**
* @保存响应头信息
*/
private $response_header_;
/**
* @body_ 用于保存curl请求返回的结果
*/
private $body_;
/**
* @读取cookie
*/
private $cookie_file_;
/**
* @写入cookie
*/
private $cookie_jar_;
/**
* @todo proxy
* @构造函数,初始化CURL回话
*/
public function Start($url){
$this->ch_ = curl_init($url);
curl_setopt($this->ch_, CURLOPT_HEADER, 1);
curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 );
}
/**
* @返回响应头
*/
public function ResponseHeader($url){
if (!function_exists('http_parse_headers')) {
function http_parse_headers ($raw_headers){
$headers = array();
foreach (explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if(!isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} else if(is_array($headers[$h[0]])) {
$tmp = array_merge($headers[$h[0]],array(trim($h[1])));
$headers[$h[0]] = $tmp;
} else {
$tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
$headers[$h[0]] = $tmp;
}
}
}
return $headers;
}
}
$this->Start($url);
curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
$this->body_=$this->Execx();
$header_size = curl_getinfo($this->ch_, CURLINFO_HEADER_SIZE);
$this->response_header_ = substr($this->body_, $start = 0, $offset = $header_size);
$this->response_header_ = http_parse_headers($this->response_header_);
print_r($this->response_header_);
return $this->Close($this->body_);
}
/**
* @读取cookie
*/
public function LoadCookie($url,$cookie_file){
$this->Start($url);
curl_setopt($this->ch_, CURLOPT_COOKIE, 1);
curl_setopt($this->ch_, CURLOPT_COOKIEFILE , $cookie_file);
$this->body_=$this->Execx();
return $this->Close($this->body_);
}
/**
* @写入cookie
*/
public function SaveCookie($url){
$this->Start($url);
curl_setopt($this->ch_, CURLOPT_COOKIE, 1);
curl_setopt($this->ch_, CURLOPT_COOKIEFILE ,'cookie.txt');
curl_setopt($this->ch_, CURLOPT_COOKIEJAR , 'cookie.txt');
$this->body_=$this->Execx();
return $this->Close($this->body_);
}
/**
* @设置HEADER
*/
public function SetHeader($headers = null){
if (is_array($headers) && count($headers) > 0) {
curl_setopt($this->ch_, CURLOPT_HTTPHEADER, $headers);
}
}
/**
* @GET请求
*/
public function Get($url, array $params = array()) {
if ($params) {
if (strpos($url, '?')) {
$url .= "&".http_build_query($params);
}
else {
$url .= "?".http_build_query($params);
}
}
$this->Start($url);
curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
if (strpos($url, 'https') === 0) {
curl_setopt($this->ch_, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
}
$this->body_=$this->Execx();
return $this->Close($this->body_);
}
/**
* @POST请求
*/
public function Post($url, array $params = array()) {
$this->Start($url);
curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->ch_, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($this->ch_, CURLOPT_POST, true);
curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
if ($params) {
curl_setopt($this->ch_, CURLOPT_POSTFIELDS, http_build_query($params));
}
$this->body_=$this->Execx();
return $this->Close($this->body_);
}
/**
* @tips: google http head 方法
*/
public function Head($url, array $params = array()) {
$this->Start($url);
curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 0);
curl_setOpt($this->ch_,CURLOPT_NOBODY, true);
$this->body_=$this->Execx();
return $this->Close($this->body_);
}
/**
* @执行CURL会话
*/
public function Execx(){
return curl_exec($this->ch_);
}
/**
* @关闭CURL句柄
*/
public function Close($body_){
if ($body_ === false) {
echo "CURL Error: " . curl_error($body_);
return false;
}
curl_close($this->ch_);
return $body_;
}
}
希望本文所述对大家的php程序设计有所帮助。


猜你喜欢
- 写入Excel中后有显示第一列客户款号总库存这些,开始写在第12行第一列开始写入,一行写入5个,然后再隔12行,再写入下边的数据,图片需要对
- 本文实例讲述了Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息。分享给大家供大家参考,具体如下:说明:此代码用于接
- 最近,带领我的学生进行一个URTP项目设计,需要进行人脸识别。由于现在的OpenCV已经到了2.X版本,因此就不想用原来的1.X版本的代码,
- 数组元素两两相加count = 0c2 = []for i in range(len(c)): if count
- 如果没有读过上面内容的读者,有兴趣的可以一阅。在上面的使用JdbcTemplate一文中,主要通过spring提供的JdbcTemplate
- 我使用的Python3.5,32版本win764位系统,pandas0.19版本,使用df=pd.read_clipboard()的时候读不
- 前言本文通过示例给大家详细介绍了关于python打印三角形的相关,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧1、直角三角
- 一、定时器概述window对象提供了两个方法来实现定时器的效果,分别是window.setTimeout()和window.setInter
- 问题如何遍历一个二叉树遍历二叉树就是访问二叉树的每一个节点二叉树父结点下先左访问,先序遍历(根左右)例如:遍历以下的二叉树遍历结果:ABDE
- 对于需要大量使用 http 请求的项目,我们通常会选择对 http 请求的方法进行二次封装,以便增加统一的 * ,或者统一处理阻止重复提交之
- 现在我们用python代码实现感知器算法。# -*- coding: utf-8 -*-import numpy as npclass Pe
- 昨天,一同事发过来的一道数据库题目,就是哪种经典的父子级 ID 在同一数据库表中设计类型。需要在原表中添加一个字段,同时,将该节点的父子级详
- 灵感来源之前在B站看到一个有意思的视频:【B站】【亦】终极云游戏!五千人同开一辆车,复现经典群体智慧实验大家可以看看,很有意思。up主通过代
- 本文为多篇“HTTP请求头相关文章”及《HTTP权威指南》一书的阅读后个人汇总整理版,以便于理解。通常HTTP消息包括客户机向服务器的请求消
- 一. torch.cat()函数解析1. 函数说明1.1 官网:torch.cat(),函数定义及参数说明如下图所示:1.2 函数功能函数将
- 本文实例讲述了javascript获取select值的方法。分享给大家供大家参考。具体分析如下:1. 获取显示的汉字document.get
- 如何计算方差简单展示一下pandas里怎么计算方差:官方文档:def def_std(df): for ix,row in df
- 如下所示:import dateutildef before_month_lastday(ti): today=dateutil
- 前言根据《2021年Stackoverflow开发者调查》,SQL是最常用的五种编程语言之一。所以,我们应该多投入时间来学习SQL。由Sto
- 一、目的之前在博文SQL Server数据库最小宕机迁移方案中提到了使用了完全备份+差异备份的功能完成了数据库的转移,但是这个方法在遇到了7