PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/commonshell/php_script/curl.php

http://commonshell.googlecode.com/
PHP | 313 lines | 197 code | 5 blank | 111 comment | 31 complexity | 7fd3be1d60f711887da8e55d63b63b32 MD5 | raw file
  1. <?php
  2. /**
  3. * @package pumpkin
  4. * @subpackage test
  5. * @author yangyu
  6. * @file $RCSfile: ZZTest.class.php,v $
  7. * @version $Revision: 1.25 $
  8. * @modifiedby $Author: yangyu $
  9. * @lastmodified $Date: 2009/04/27 02:29:15 $
  10. * @copyright Copyright (c) 2005, eYou.com
  11. * ??????php????url???
  12. */
  13. define("LOG_PATH", "/home/gao/logs/");
  14. class ZZTest{
  15. var $mClassName = null;
  16. var $mDomain = null;
  17. var $mLoginUrl = null;
  18. var $mUrl = null;
  19. var $mCookFile = null;
  20. var $mIsOutputHeader= false;//????head
  21. var $mIsNoBody = false; //?????body
  22. var $mIsLocation = false;//??????
  23. var $mVerbose = false; //????
  24. var $mReturntransfer= 1; //
  25. var $mTimeout = 30; //??curl?????????
  26. var $mReturnHtml = null; //???html??
  27. var $mInfo = null; //curl_getinfo???
  28. var $mProxy = null; //??ip
  29. var $mMethod = "post";
  30. private $mSslCert = null; //ssl ????string
  31. private $mSslCertPasswd = null;//ssl ??????
  32. private $mSslKey = null; //ssl ????string
  33. private $mSslKeyPasswd = null;//ssl ??????
  34. private $mSslCaFile = null;
  35. private $mSslCheck = false;
  36. /** {{{http ?
  37. *$this_header = array(
  38. * "MIME-Version: 1.0",
  39. * "Content-type: text/html; charset=iso-8859-1",
  40. * "Content-transfer-encoding: text"
  41. * );
  42. */
  43. var $mHttpHeader = null;
  44. //}}}
  45. var $mReferer = null;
  46. var $hZZTestLogin = null;//????????
  47. /** {{{ ???? __construct()
  48. */
  49. function __construct(){
  50. $this->mClassName = __CLASS__;
  51. }//}}}
  52. /** {{{ curl?????? login($url,$request)
  53. * ?????????????post????get??url????
  54. * @param string ???url
  55. * @param array $request=null????post??
  56. * @return string
  57. * @see ZZTest::get
  58. * @see ZZTest::post
  59. */
  60. function login($url,$request=null){
  61. $this->mCookFile = tempnam('/tmp','cookie');
  62. if(file_exists($this->mCookFile)){//????
  63. $fp = @fopen($this->mCookFile,"r+");
  64. $ok = @ftruncate($fp,'0');
  65. @fclose($fp);
  66. if($ok == false){
  67. $this->setMsg('????cookie??');
  68. return false;
  69. }
  70. }
  71. $ch = curl_init();
  72. if($this->mMethod=="post"){//??get????
  73. $this->mReturnHtml = $this->post($url,$request);
  74. }else{
  75. if(false===strpos($url,"?")){
  76. $url .= "?".$this->dataEncode($request);
  77. }else{
  78. $url .= "&".$this->dataEncode($request);
  79. }
  80. $this->mReturnHtml = $this->get($url);
  81. }
  82. return $this->mReturnHtml;
  83. }//}}}
  84. /** {{{ ???url ???? post($url,$post=null)
  85. * @param string ????url ??
  86. * @param array ?post???
  87. * @return string
  88. */
  89. function post($url,$post= null ){
  90. if($post !== null && (false == ($post = $this->dataEncode($post)))){
  91. $this->setMsg('post ??????');
  92. return false;
  93. }
  94. $ch = curl_init();
  95. curl_setopt($ch, CURLOPT_URL, $url);
  96. curl_setopt($ch, CURLOPT_HEADER, $this->mIsOutputHeader);
  97. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->mIsLocation);
  98. curl_setopt($ch, CURLOPT_VERBOSE, $this->mVerbose);//???????
  99. curl_setopt($ch, CURLOPT_COOKIEJAR, $this->mCookFile); //?????????????
  100. curl_setopt($ch, CURLOPT_POST, 1);
  101. curl_setopt($ch, CURLOPT_TIMEOUT, $this->mTimeout);
  102. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  104. curl_setopt($ch, CURLOPT_COOKIEFILE, $this->mCookFile);
  105. if(null != $this->mReferer) curl_setopt($ch, CURLOPT_REFERER, $this->mReferer);
  106. if(is_array($this->mHttpHeader)) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->mHttpHeader);
  107. //{{{ ssl
  108. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,$this->mSslCheck);
  109. /*
  110. if(!empty($this->mSslCert)) curl_setopt($ch,CURLOPT_SSLCERT,$this->mSslCert);
  111. if(!empty($this->mSslCertPasswd)) curl_setopt($ch,CURLOPT_SSLCERTPASSWD,$this->mSslCertPasswd);
  112. if(!empty($this->mSslKey)) curl_setopt($ch,CURLOPT_SSLKEY,$this->mSslKey);
  113. if(!empty($this->mSslKeyPasswd)) curl_setopt($ch,CURLOPT_SSLKEYPASSWD,$this->mSslKeyPasswd);
  114. if(!empty($this->mSslCaFile)) curl_setopt($ch,CURLOPT_CAINFO,$this->mSslCaFile);
  115. */
  116. //}}}
  117. $msg = "??url:".$url;
  118. $orders = curl_exec($ch);
  119. if ($ok = curl_errno($ch)) {
  120. $this->setMsg(curl_error($ch));
  121. $msg .= "??";
  122. $this->log($msg);
  123. return false;
  124. }
  125. curl_close($ch);
  126. $msg .= "??";
  127. $this->log($msg);
  128. return $orders;
  129. }//}}}
  130. /** {{{ ???url GET???? get($url)
  131. * @param string ????url ??
  132. * @return string ????url???html??
  133. */
  134. function get($url){
  135. $ch = curl_init();
  136. curl_setopt($ch, CURLOPT_URL, $url);
  137. curl_setopt($ch, CURLOPT_HEADER, $this->mIsOutputHeader);
  138. curl_setopt($ch, CURLOPT_POST, 0);
  139. curl_setopt($ch, CURLOPT_AUTOREFERER,true);
  140. curl_setopt($ch, CURLOPT_COOKIEJAR, $this->mCookFile); //?????????????
  141. curl_setopt($ch, CURLOPT_RETURNTRANSFER, $this->mReturntransfer);
  142. curl_setopt($ch, CURLOPT_TIMEOUT, $this->mTimeout);
  143. curl_setopt($ch, CURLOPT_COOKIEFILE, $this->mCookFile);
  144. // curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  145. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->mIsLocation);
  146. curl_setopt($ch, CURLOPT_NOBODY, $this->mIsNoBody);
  147. curl_setopt($ch, CURLOPT_VERBOSE, $this->mVerbose);//???????
  148. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5))');
  149. //{{{ ssl
  150. /*
  151. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,$this->mSslCheck);
  152. if(!empty($this->mSslCert)) curl_setopt($ch,CURLOPT_SSLCERT,$this->mSslCert);
  153. if(!empty($this->mSslCertPasswd)) curl_setopt($ch,CURLOPT_SSLCERTPASSWD,$this->mSslCertPasswd);
  154. if(!empty($this->mSslKey)) curl_setopt($ch,CURLOPT_SSLKEY,$this->mSslKey);
  155. if(!empty($this->mSslKeyPasswd)) curl_setopt($ch,CURLOPT_SSLKEYPASSWD,$this->mSslKeyPasswd);
  156. if(!empty($this->mSslCaFile)) curl_setopt($ch,CURLOPT_CAINFO,$this->mSslCaFile);
  157. */
  158. //}}}
  159. if($this->mProxy){
  160. curl_setopt($ch, CURLOPT_PROXY,$this->mProxy);
  161. curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
  162. }
  163. if(null != $this->mReferer) curl_setopt($ch, CURLOPT_REFERER, $this->mReferer);
  164. if(__CLASS__ != $this->mClassName) curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this->mClassName,'readHeader'));
  165. if(is_array($this->mHttpHeader)) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->mHttpHeader);
  166. $msg = "??url:".$url;
  167. $orders = curl_exec($ch);
  168. if ($ok = curl_errno($ch)) {
  169. $this->setMsg(curl_error($ch));
  170. $msg .= "??";
  171. $this->log($msg);
  172. return false;
  173. }
  174. $this->mInfo = curl_getinfo($ch);
  175. $msg .= "??";
  176. $this->log($msg);
  177. curl_close($ch);
  178. return $orders;
  179. }//}}}
  180. /** {{{ ???post??? dataEncode($data,$keyprefix,$keyprefix);
  181. */
  182. function dataEncode($data, $keyprefix = "", $keypostfix = "") {
  183. if(is_array($data)) return http_build_query($data);
  184. else{
  185. return $data;
  186. }
  187. }//}}}
  188. /** {{{ ????????? setLoginObj($obj)
  189. * ????????post???????????
  190. * ?????????????????????
  191. * <code>
  192. * <?php
  193. * //??????
  194. * $email = 'yangyu@eyou.net';
  195. * //?????????????email,pass
  196. * $test = new ZZTestLogin($email);
  197. * $ok = $test->login();//true ????
  198. *
  199. * //?????????,??????????????????setLoingObj
  200. * $friend = new TestFriend($test);
  201. * $ok = $friend->addFriend($user_id_b);//????
  202. * $msg = $friend->getMsg();
  203. * $this->assertTrue($ok,$msg);
  204. *
  205. * ?>
  206. * </code>
  207. *
  208. */
  209. function setLoginObj($obj){
  210. if($obj instanceof ZZTestLogin){
  211. $this->hZZTestLogin = $obj;
  212. return true;
  213. }
  214. else{
  215. $this->setMsg("??????????? ZZTestLogin ????");
  216. return false;
  217. }
  218. }//}}}
  219. /** {{{ ????????? setDomain($domain)
  220. * <code>
  221. * </code>
  222. *
  223. */
  224. function setDomain($domain){
  225. $this->mDomain = $domain;
  226. }//}}}
  227. /** {{{ ???????URL setURL($url)
  228. * <code>
  229. * </code>
  230. *
  231. */
  232. function setURL($url){
  233. if(empty($this->mDomain)){
  234. return false;
  235. }
  236. $this->mUrl = $this->mDomain."/".$url;
  237. }//}}}
  238. /** {{{ ???? readHeader($ch,$string)
  239. */
  240. function readHeader($ch,$string){
  241. ;
  242. }//}}}
  243. /** {{{ ????? parseHeader($header)
  244. * @param string
  245. * @return array|false
  246. */
  247. function parseHeader($header){
  248. $pos = strpos($header,"\r\n\r\n");
  249. $header = substr($header,0,$pos);
  250. $h = explode("\r\n",$header);
  251. if(is_array($h)){
  252. $r = array();
  253. foreach($h as $k=>$v){
  254. $tmp = explode(":",$v,2);
  255. if(2 == count($tmp)){
  256. if('Date' == $tmp[0]) $r['time'] = strtotime($tmp[1]);
  257. $r[$tmp[0]] = trim($tmp[1]);
  258. }
  259. }
  260. return $r;
  261. }
  262. return false;
  263. }//}}}
  264. /** {{{ ??ssl?????? setSslKey($filename,$passwd = '')
  265. *
  266. */
  267. function setSslKey($filename,$passwd){
  268. if(file_exists($filename)){
  269. $this->mSslKey = $filename;
  270. }
  271. $this->mSslKeyPasswd = $passwd;
  272. }//}}}
  273. /** {{{ ??ssl cert setSslCert($filename,$passwd = '')
  274. *
  275. */
  276. function setSslCert($filename,$passwd){
  277. var_dump($filename);
  278. if(file_exists($filename)){
  279. $this->mSslCert = $filename;
  280. }
  281. $this->mSslCertPasswd = $passwd;
  282. }//}}}
  283. /** {{{ ??ssl cert setSslCaFile($filename)
  284. *
  285. */
  286. function setSslCaFile($filename){
  287. if(file_exists($filename)){
  288. $this->mSslCaFile= $filename;
  289. }
  290. }//}}}
  291. /** {{{ ??log log($msg)
  292. * @param string ????
  293. * @return void
  294. */
  295. function log($msg,$logfile = null){
  296. $date = date("Y-m-d H:i:s");
  297. $filedate = date("Y_m_d");
  298. $msg = "[".$date."]:".$msg."\n";
  299. $msg = mb_convert_encoding($msg,'GBK','UTF-8');
  300. // echo $msg;
  301. if(empty($logfile)) $logfile = "zztest_".$filedate.".log";
  302. @error_log($msg,3,LOG_PATH."/".$logfile);
  303. }//}}}
  304. function setMsg($msg){
  305. return true;
  306. }
  307. }//end class
  308. ?>