php的curl封装类用法实例
程序员文章站
2023-04-06 07:57:18
本文实例讲述了两个php curl封装类的用法实例,这两个函数可以让我们非常的方便的使用php curl相关函数。分享给大家供大家参考。具体如下:
使用函数之前我们要需要...
本文实例讲述了两个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);
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')));
}
}
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;
}
}
?>
//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('//www.jb51.net/');
$curl->get('//www.jb51.net/');
post用法:
复制代码 代码如下:
$curl = new curl();
$curl->get('//www.jb51.net/', 'p=1&time=0′);
$curl->get('//www.jb51.net/', 'p=1&time=0′);
希望本文所述对大家的php程序设计有所帮助。
下一篇: redis 系列7 数据结构之跳跃表