本文实例讲述了两个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<br />include_once('curl.class.php');<br />$aa =new Curl('');<br /> $curlOptions = array(<br /> CURLOPT_URL => "http://www.xx.com/addTicket.jsp", //访问URL<br /> CURLOPT_RETURNTRANSFER => true, //获取结果作为字符串返回<br /> CURLOPT_REFERER => "ww.ww.ww/zw2",<br /> 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' ),<br /> CURLOPT_HEADER => 1, //获取返回头信息<br /> //CURLOPT_SSL_VERIFYPEER => false, //支持SSL加密<br /> CURLOPT_POST => true, //发送时带有POST参数<br /> CURLOPT_POSTFIELDS => 'ids=897&Submit=%E6%8A%95%E7%A5%A8', //请求的POST参数字符串<br /> CURLOPT_TIMEOUT => $aa->timeout //等待响应的时间<br /> );<br /> echo $aa->getResponseText($curlOptions);
cul处理类:
<?php<br />class Curl<br />{<br />public $cookieFile;<br />public $timeout = 160;<br />Public function __construct($dir){<br />$this->cookieFile = $this->getTemporaryCookieFileName($dir);<br />}<br />/**<br />* 设置CURL参数并发送请求,获取响应内容<br />* @access private<br />* @param $curlOptions array curl设置参数数组<br />* @return string|false 访问成功,按字符串形式返回获取的信息;否则返回false<br />*/<br />public function getResponseText($curlOptions) {<br />/* 设置CURLOPT_RETURNTRANSFER为true */<br />if(!isset($curlOptions[CURLOPT_RETURNTRANSFER]) || $curlOptions[CURLOPT_RETURNTRANSFER] == false) {<br />$curlOptions[CURLOPT_RETURNTRANSFER] = true;<br />}<br />/* 初始化curl模块 */<br />$curl = curl_init();<br />/* 设置curl选项 */<br />curl_setopt_array($curl, $curlOptions);<br />/* 发送请求并获取响应信息 */<br />$responseText = '';<br />try {<br />$responseText = curl_exec($curl);<br />if(($errno = curl_errno($curl)) != CURLM_OK) {<br />$errmsg = curl_error($curl);<br />throw new Exception($errmsg, $errno);<br />}<br />} catch (Exception $e) {<br />//exceptionDisposeFunction($e);<br />//print_r($e);<br />$responseText = false;<br />}<br />/* 关闭curl模块 */<br />curl_close($curl);<br />/* 返回结果 */<br />return $responseText;<br />}<br />/**<br />* 将Unicode字符串(u0000)转化为utf-8字符串,工具函数<br />* @access private<br />* @static<br />* @param <mark>6来源gaodaimacom搞#^代%!码网</mark><strong>搞gaodaima代码</strong>$string string Unicode字符串<br />* @return string utf-8字符串<br />*/<br />public function unicodeToUtf8($string) {<br />$string = str_replace('u', '', strtolower($string));<br />$length = strlen($string) / 4;<br />$stringResult = '';<br />for($i = 0; $i < $length; $i++) {<br />$charUnicodeHex = substr($string, $i * 4, 4);<br />$unicodeCode = hexdec($charUnicodeHex);<br />$utf8Code = '';<br />if($unicodeCode < 128) {<br />$utf8Code = chr($unicodeCode);<br />} else if($unicodeCode < 2048) {<br />$utf8Code .= chr(192 + (($unicodeCode - ($unicodeCode % 64)) / 64));<br />$utf8Code .= chr(128 + ($unicodeCode % 64));<br />} else {<br />$utf8Code .= chr(224 + (($unicodeCode - ($unicodeCode % 4096)) / 4096));<br />$utf8Code .= chr(128 + ((($unicodeCode % 4096) - ($unicodeCode % 64)) / 64));<br />$utf8Code .= chr(128 + ($unicodeCode % 64));<br />}<br />$stringResult .= $utf8Code;<br />}<br />return $stringResult;<br />}<br />private function getTemporaryCookieFileName($dir='.') {<br />return (str_replace("", '/', tempnam($dir, 'tmp')));<br />}<br />}
例子2