<?php

/**
 * httphttpsͨ
 * ============================================================================
 * api˵
 * setReqContent($reqContent),ݣpostgetgetʽṩ
 * getResContent(), ȡӦ
 * setMethod($method),󷽷,postget
 * getErrInfo(),ȡϢ
 * setCertInfo($certFile, $certPasswd, $certType="PEM"),֤飬˫httpsʱҪʹ
 * setCaInfo($caFile), CAʽδpem򲻼
 * setTimeOut($timeOut) óʱʱ䣬λ
 * getResponseCode(), ȡصhttp״̬
 * call(),ýӿ
 * 
 * ============================================================================
 *
 */

class TenpayHttpClient {
	//ݣpostgetgetʽṩ
	var $reqContent;
	//Ӧ
	var $resContent;
	//󷽷
	var $method;
	
	//֤ļ
	var $certFile;
	//֤
	var $certPasswd;
	//֤PEM
	var	$certType;
	
	//CAļ
	var $caFile;
	
	//Ϣ
	var $errInfo;
	
	//ʱʱ
	var $timeOut;
	
	//http״̬
	var $responseCode;
	
	function __construct() {
		$this->TenpayHttpClient();
	}
	
	
	function TenpayHttpClient() {
		$this->reqContent = "";
		$this->resContent = "";
		$this->method = "post";

		$this->certFile = "";
		$this->certPasswd = "";
		$this->certType = "PEM";
		
		$this->caFile = "";
		
		$this->errInfo = "";
		
		$this->timeOut = 120;
		
		$this->responseCode = 0;
		
	}
	
	
	//
	function setReqContent($reqContent) {
		$this->reqContent = $reqContent;
	}
	
	//ȡ
	function getResContent() {
		return $this->resContent;
	}
	
	//󷽷postget	
	function setMethod($method) {
		$this->method = $method;
	}
	
	//ȡϢ
	function getErrInfo() {
		return $this->errInfo;
	}
	
	//֤Ϣ
	function setCertInfo($certFile, $certPasswd, $certType="PEM") {
		$this->certFile = $certFile;
		$this->certPasswd = $certPasswd;
		$this->certType = $certType;
	}
	
	//Ca
	function setCaInfo($caFile) {
		$this->caFile = $caFile;
	}
	
	//óʱʱ,λ
	function setTimeOut($timeOut) {
		$this->timeOut = $timeOut;
	}
	
	//ִhttp
	function call() {
		//һCURLỰ
		$ch = curl_init();

		// curlִе
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeOut);

		// ȡϢļʽأֱ
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

		// ֤мSSL㷨Ƿ
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
				
		
		$arr = explode("?", $this->reqContent);
		if(count($arr) >= 2 && $this->method == "post") {
			//һPOSTΪapplication/x-www-form-urlencodedύһ
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_URL, $arr[0]);
			//Ҫ͵
			curl_setopt($ch, CURLOPT_POSTFIELDS, $arr[1]);
		
		}else{
			curl_setopt($ch, CURLOPT_URL, $this->reqContent);
		}
		
		//֤Ϣ
		if($this->certFile != "") {
			curl_setopt($ch, CURLOPT_SSLCERT, $this->certFile);
			curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $this->certPasswd);
			curl_setopt($ch, CURLOPT_SSLCERTTYPE, $this->certType);
		}
		
		//CA
		if($this->caFile != "") {
			// ֤֤Դļ飬0ʾֹ֤ĺϷԵļ顣1ҪCURLOPT_CAINFO
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
			curl_setopt($ch, CURLOPT_CAINFO, $this->caFile);
		} else {
			// ֤֤Դļ飬0ʾֹ֤ĺϷԵļ顣1ҪCURLOPT_CAINFO
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		}
		
		// ִв
		$res = curl_exec($ch);
		$this->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		
		if ($res == NULL) { 
		   $this->errInfo = "call http err :" . curl_errno($ch) . " - " . curl_error($ch) ;
		   curl_close($ch);
		   return false;
		} else if($this->responseCode  != "200") {
			$this->errInfo = "call http err httpcode=" . $this->responseCode  ;
			curl_close($ch);
			return false;
		}
		
		curl_close($ch);
		$this->resContent = $res;

		
		return true;
	}
	
	function getResponseCode() {
		return $this->responseCode;
	}
	
}
?>