/commonshell/php_script/curl.php
PHP | 313 lines | 197 code | 5 blank | 111 comment | 31 complexity | 7fd3be1d60f711887da8e55d63b63b32 MD5 | raw file
- <?php
- /**
- * @package pumpkin
- * @subpackage test
- * @author yangyu
- * @file $RCSfile: ZZTest.class.php,v $
- * @version $Revision: 1.25 $
- * @modifiedby $Author: yangyu $
- * @lastmodified $Date: 2009/04/27 02:29:15 $
- * @copyright Copyright (c) 2005, eYou.com
- * ??????php????url???
- */
- define("LOG_PATH", "/home/gao/logs/");
- class ZZTest{
- var $mClassName = null;
- var $mDomain = null;
- var $mLoginUrl = null;
- var $mUrl = null;
- var $mCookFile = null;
- var $mIsOutputHeader= false;//????head
- var $mIsNoBody = false; //?????body
- var $mIsLocation = false;//??????
- var $mVerbose = false; //????
- var $mReturntransfer= 1; //
- var $mTimeout = 30; //??curl?????????
- var $mReturnHtml = null; //???html??
- var $mInfo = null; //curl_getinfo???
- var $mProxy = null; //??ip
- var $mMethod = "post";
- private $mSslCert = null; //ssl ????string
- private $mSslCertPasswd = null;//ssl ??????
- private $mSslKey = null; //ssl ????string
- private $mSslKeyPasswd = null;//ssl ??????
- private $mSslCaFile = null;
- private $mSslCheck = false;
- /** {{{http ?
- *$this_header = array(
- * "MIME-Version: 1.0",
- * "Content-type: text/html; charset=iso-8859-1",
- * "Content-transfer-encoding: text"
- * );
- */
- var $mHttpHeader = null;
- //}}}
- var $mReferer = null;
- var $hZZTestLogin = null;//????????
- /** {{{ ???? __construct()
- */
- function __construct(){
- $this->mClassName = __CLASS__;
- }//}}}
- /** {{{ curl?????? login($url,$request)
- * ?????????????post????get??url????
- * @param string ???url
- * @param array $request=null????post??
- * @return string
- * @see ZZTest::get
- * @see ZZTest::post
- */
- function login($url,$request=null){
- $this->mCookFile = tempnam('/tmp','cookie');
- if(file_exists($this->mCookFile)){//????
- $fp = @fopen($this->mCookFile,"r+");
- $ok = @ftruncate($fp,'0');
- @fclose($fp);
- if($ok == false){
- $this->setMsg('????cookie??');
- return false;
- }
- }
- $ch = curl_init();
- if($this->mMethod=="post"){//??get????
- $this->mReturnHtml = $this->post($url,$request);
- }else{
- if(false===strpos($url,"?")){
- $url .= "?".$this->dataEncode($request);
- }else{
- $url .= "&".$this->dataEncode($request);
- }
- $this->mReturnHtml = $this->get($url);
- }
- return $this->mReturnHtml;
- }//}}}
- /** {{{ ???url ???? post($url,$post=null)
- * @param string ????url ??
- * @param array ?post???
- * @return string
- */
- function post($url,$post= null ){
- if($post !== null && (false == ($post = $this->dataEncode($post)))){
- $this->setMsg('post ??????');
- return false;
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, $this->mIsOutputHeader);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->mIsLocation);
- curl_setopt($ch, CURLOPT_VERBOSE, $this->mVerbose);//???????
- curl_setopt($ch, CURLOPT_COOKIEJAR, $this->mCookFile); //?????????????
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, $this->mTimeout);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $this->mCookFile);
- if(null != $this->mReferer) curl_setopt($ch, CURLOPT_REFERER, $this->mReferer);
- if(is_array($this->mHttpHeader)) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->mHttpHeader);
- //{{{ ssl
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,$this->mSslCheck);
- /*
- if(!empty($this->mSslCert)) curl_setopt($ch,CURLOPT_SSLCERT,$this->mSslCert);
- if(!empty($this->mSslCertPasswd)) curl_setopt($ch,CURLOPT_SSLCERTPASSWD,$this->mSslCertPasswd);
- if(!empty($this->mSslKey)) curl_setopt($ch,CURLOPT_SSLKEY,$this->mSslKey);
- if(!empty($this->mSslKeyPasswd)) curl_setopt($ch,CURLOPT_SSLKEYPASSWD,$this->mSslKeyPasswd);
- if(!empty($this->mSslCaFile)) curl_setopt($ch,CURLOPT_CAINFO,$this->mSslCaFile);
- */
- //}}}
- $msg = "??url:".$url;
- $orders = curl_exec($ch);
- if ($ok = curl_errno($ch)) {
- $this->setMsg(curl_error($ch));
- $msg .= "??";
- $this->log($msg);
- return false;
- }
- curl_close($ch);
- $msg .= "??";
- $this->log($msg);
- return $orders;
- }//}}}
- /** {{{ ???url GET???? get($url)
- * @param string ????url ??
- * @return string ????url???html??
- */
- function get($url){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, $this->mIsOutputHeader);
- curl_setopt($ch, CURLOPT_POST, 0);
- curl_setopt($ch, CURLOPT_AUTOREFERER,true);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $this->mCookFile); //?????????????
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, $this->mReturntransfer);
- curl_setopt($ch, CURLOPT_TIMEOUT, $this->mTimeout);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $this->mCookFile);
- // curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->mIsLocation);
- curl_setopt($ch, CURLOPT_NOBODY, $this->mIsNoBody);
- curl_setopt($ch, CURLOPT_VERBOSE, $this->mVerbose);//???????
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5))');
- //{{{ ssl
- /*
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,$this->mSslCheck);
- if(!empty($this->mSslCert)) curl_setopt($ch,CURLOPT_SSLCERT,$this->mSslCert);
- if(!empty($this->mSslCertPasswd)) curl_setopt($ch,CURLOPT_SSLCERTPASSWD,$this->mSslCertPasswd);
- if(!empty($this->mSslKey)) curl_setopt($ch,CURLOPT_SSLKEY,$this->mSslKey);
- if(!empty($this->mSslKeyPasswd)) curl_setopt($ch,CURLOPT_SSLKEYPASSWD,$this->mSslKeyPasswd);
- if(!empty($this->mSslCaFile)) curl_setopt($ch,CURLOPT_CAINFO,$this->mSslCaFile);
- */
- //}}}
- if($this->mProxy){
- curl_setopt($ch, CURLOPT_PROXY,$this->mProxy);
- curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
- }
- if(null != $this->mReferer) curl_setopt($ch, CURLOPT_REFERER, $this->mReferer);
- if(__CLASS__ != $this->mClassName) curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this->mClassName,'readHeader'));
- if(is_array($this->mHttpHeader)) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->mHttpHeader);
- $msg = "??url:".$url;
- $orders = curl_exec($ch);
- if ($ok = curl_errno($ch)) {
- $this->setMsg(curl_error($ch));
- $msg .= "??";
- $this->log($msg);
- return false;
- }
- $this->mInfo = curl_getinfo($ch);
- $msg .= "??";
- $this->log($msg);
- curl_close($ch);
- return $orders;
- }//}}}
- /** {{{ ???post??? dataEncode($data,$keyprefix,$keyprefix);
- */
- function dataEncode($data, $keyprefix = "", $keypostfix = "") {
- if(is_array($data)) return http_build_query($data);
- else{
- return $data;
- }
- }//}}}
- /** {{{ ????????? setLoginObj($obj)
- * ????????post???????????
- * ?????????????????????
- * <code>
- * <?php
- * //??????
- * $email = 'yangyu@eyou.net';
- * //?????????????email,pass
- * $test = new ZZTestLogin($email);
- * $ok = $test->login();//true ????
- *
- * //?????????,??????????????????setLoingObj
- * $friend = new TestFriend($test);
- * $ok = $friend->addFriend($user_id_b);//????
- * $msg = $friend->getMsg();
- * $this->assertTrue($ok,$msg);
- *
- * ?>
- * </code>
- *
- */
- function setLoginObj($obj){
- if($obj instanceof ZZTestLogin){
- $this->hZZTestLogin = $obj;
- return true;
- }
- else{
- $this->setMsg("??????????? ZZTestLogin ????");
- return false;
- }
- }//}}}
- /** {{{ ????????? setDomain($domain)
- * <code>
- * </code>
- *
- */
- function setDomain($domain){
- $this->mDomain = $domain;
- }//}}}
- /** {{{ ???????URL setURL($url)
- * <code>
- * </code>
- *
- */
- function setURL($url){
- if(empty($this->mDomain)){
- return false;
- }
- $this->mUrl = $this->mDomain."/".$url;
- }//}}}
- /** {{{ ???? readHeader($ch,$string)
- */
- function readHeader($ch,$string){
- ;
- }//}}}
- /** {{{ ????? parseHeader($header)
- * @param string
- * @return array|false
- */
- function parseHeader($header){
- $pos = strpos($header,"\r\n\r\n");
- $header = substr($header,0,$pos);
- $h = explode("\r\n",$header);
- if(is_array($h)){
- $r = array();
- foreach($h as $k=>$v){
- $tmp = explode(":",$v,2);
- if(2 == count($tmp)){
- if('Date' == $tmp[0]) $r['time'] = strtotime($tmp[1]);
- $r[$tmp[0]] = trim($tmp[1]);
- }
- }
- return $r;
- }
- return false;
- }//}}}
- /** {{{ ??ssl?????? setSslKey($filename,$passwd = '')
- *
- */
- function setSslKey($filename,$passwd){
- if(file_exists($filename)){
- $this->mSslKey = $filename;
- }
- $this->mSslKeyPasswd = $passwd;
- }//}}}
- /** {{{ ??ssl cert setSslCert($filename,$passwd = '')
- *
- */
- function setSslCert($filename,$passwd){
- var_dump($filename);
- if(file_exists($filename)){
- $this->mSslCert = $filename;
- }
- $this->mSslCertPasswd = $passwd;
- }//}}}
- /** {{{ ??ssl cert setSslCaFile($filename)
- *
- */
- function setSslCaFile($filename){
- if(file_exists($filename)){
- $this->mSslCaFile= $filename;
- }
- }//}}}
- /** {{{ ??log log($msg)
- * @param string ????
- * @return void
- */
- function log($msg,$logfile = null){
- $date = date("Y-m-d H:i:s");
- $filedate = date("Y_m_d");
- $msg = "[".$date."]:".$msg."\n";
- $msg = mb_convert_encoding($msg,'GBK','UTF-8');
- // echo $msg;
- if(empty($logfile)) $logfile = "zztest_".$filedate.".log";
- @error_log($msg,3,LOG_PATH."/".$logfile);
- }//}}}
- function setMsg($msg){
- return true;
- }
- }//end class
- ?>