这篇文章主要介绍了php的curl封装类用法,以实例形式较为详细的讲述了curl封装类及其使用方法,并总结了GET与POST的用法,需要的朋友可以参考下
本文实例讲述了两个php curl封装类的用法实例,这两个函数可以让我们非常的方便的使用php curl相关函数。分享给大家供大家参考。具体如下:
使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll)
开启php curl函数库的步骤
1).去掉windows/php.ini 文件里;extension=php_curl.dll前面的; /*用 echo phpinfo();查看php.ini的路径*/
2).把php5/libeay32.dll,ssleay32.dll复制到系统目录windows/下
3).重启apache
代码如下:
<?php include_once('curl.class.php'); $aa =new Curl(''); $curlOptions = array( CURLOPT_URL => "http://www.xx.com/addTicket.jsp", //访问URL CURLOPT_RETURNTRANSFER => true, //获取结果作为字符串返回 CURLOPT_REFERER => "ww.ww.ww/zw2", CURLOPT_HTTPHEADER => array('X-FORWARDED-FOR:139.197.14.19', 'CLIENT-IP:127.0.0.1','Proxy-Client-IP:139.197.14.19','WL-Proxy-Client-IP:139.197.14.19' ), CURLOPT_HEADER => 1, //获取返回头信息 //CURLOPT_SSL_VERIFYPEER => false, //支持SSL加密 CURLOPT_POST => true, //发送时带有POST参数 CURLOPT_POSTFIELDS => 'ids=897&Submit=%E6%8A%95%E7%A5%A8', //请求的POST参数字符串 CURLOPT_TIMEOUT => $aa->timeout //等待响应的时间 ); echo $aa->getResponseText($curlOptions);
cul处理类:
代码如下:
<?php class Curl { public $cookieFile; public $timeout = 160; Public function __construct($dir){ $this->cookieFile = $this->getTemporaryCookieFileName($dir); } /** * 设置CURL参数并发送请求,获取响应内容 * @access private * @param $curlOptions array curl设置参数数组 * @return string|false 访问成功,按字符串形式返回获取的信息;否则返回false */ public function getResponseText($curlOptions) { /* 设置CURLOPT_RETURNTRANSFER为true */ if(!isset($curlOptions[CURLOPT_RETURNTRANSFER]) || $curlOptions[CURLOPT_RETURNTRANSFER] == false) { $curlOptions[CURLOPT_RETURNTRANSFER] = true; } /* 初始化curl模块 */ $curl = curl_init(); /* 设置curl选项 */ curl_setopt_array($curl, $curlOptions); /* 发送请求并获取响应信息 */ $responseText = ''; try { $responseText = curl_exec($curl); if(($errno = curl_errno($curl)) != CURLM_OK) { $errmsg = curl_error($curl); throw new Exception($errmsg, $errno); } } catch (Exception $e) { //exceptionDisposeFunction($e); //print_r($e); $responseText = false; } /* 关闭curl模块 */ curl_close($curl); /* 返回结果 */ return $responseText; } /** * 将Unicode字符串(u0000)转化为utf-8字符串,工具函数 * @access private * @static * @param $string string Unicode字符串 * @return string utf-8字符串 */ public function unicodeToUtf8($string) { $string = str_replace('u', '', strtolower($string)); $length = strlen($string) / 4; $stringResult = ''; for($i = 0; $i < $length; $i++) { $charUnicodeHex = substr($string, $i * 4, 4); $unicodeCode = hexdec($charUnicodeHex); $utf8Code = ''; if($unicodeCode < 128) { $utf8Code = chr($unicodeCode); } else if($unicodeCode < 2048) { $utf8Code .= chr(192 + (($unicodeCode - ($unicodeCode % 64)) / 64)); $utf8Code .= chr(128 + ($unicodeCode % 64)); } else { $utf8Code .= chr(224 + (($unicodeCode - ($unicodeCode % 4096)) / 4096)); $utf8Code .= chr(128 + ((($unicodeCode % 4096) - ($unicodeCode % 64)) / 64)); $utf8Code .= chr(128 + ($unicodeCode % 64)); } $stringResult .= $utf8Code; } return $stringResult; } private function getTemporaryCookieFileName($dir='.') { return (str_replace("", '/', tempnam($dir, 'tmp'))); } }
例子2
代码如下:
<?php //curl类 class Curl { function Curl(){ return true; } function execute($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){ $ch = Curl::create(); if(false === $ch){ return false; } if(is_string($url) && strlen($url)){ $ret = curl_setopt($ch, CURLOPT_URL, $url); }else{ return false; } //是否显示头部信息 curl_setopt($ch, CURLOPT_HEADER, false); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($username != ''){ curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); } $method = strtolower($method); if('post' == $method){ curl_setopt($ch, CURLOPT_POST, true); if(is_array($fields)){ $sets = array(); foreach ($fields AS $key => $val){ $sets[] = $key . '=' . urlencode($val); } $fields = implode('&',$sets); } curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); }else if('put' == $method){ curl_setopt($ch, CURLOPT_PUT, true); } //curl_setopt($ch, CURLOPT_PROGRESS, true); //curl_setopt($ch, CURLOPT_VERBOSE, true); //curl_setopt($ch, CURLOPT_MUTE, false); curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置curl超时秒数 if(strlen($userAgent)){ curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); } if(is_array($httpHeaders)){ curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); } $ret = curl_exec($ch); if(curl_errno($ch)){ curl_close($ch); return array(curl_error($ch), curl_errno($ch)); }else{ curl_close($ch); if(!is_string($ret) || !strlen($ret)){ return false; } return $ret; } } function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){ $ret = Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password); if(false === $ret){ return false; } if(is_array($ret)){ return false; } return $ret; } function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){ $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password); if(false === $ret){ return false; } if(is_array($ret)){ return false; } return $ret; } function create(){ $ch = null; if(!function_exists('curl_init')){ return false; } $ch = curl_init(); if(!is_resource($ch)){ return false; } return $ch; } } ?>
用法
GET用法:
代码如下:
$curl = new Curl(); $curl->get('http://www.jb51.net/');
POST用法:
代码如下:
$curl = new Curl(); $curl->get('http://www.jb51.net/', 'p=1&time=0′);

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版