args. ## This class is the base class called by the other classes. It effectively holds the ## arguments in an associative array called $this->args[varname] = varvalue. class dataArgumentContainer { protected $args = array(); protected $strtolower = true; public function __construct($args=array()) { if(count($args) > 0) { if(is_numeric(key($args))) $this->__assignArgs($args); else $this->setArray($args); } } protected function __assignArgs($args) { if(count($args) > 0) { //if(@is_array($args[0])) $this->__assignArgs($args[0]); $vars = array_keys(get_object_vars($this)); for($i=0;$i"; $this->{$args[$i]} = $args[$i+1]; } elseif(!isset($this->{$args[$i]})) { $k = trim($args[$i]); if($this->strtolower) { $k = strtolower($k); } if(isset($args[$i+1])) $this->args[$k] = $args[$i+1]; else $this->args[$k] = ""; } } } } public function __isset($var) { if(isset($this->args[$var])) { return true; } else { return false; } } public function __set($var, $value) { //echo "$var = $value
"; $this->args[$var] = $value; } public function asArray() { return $this->args; } public function setArray($arr) { foreach($arr as $key => $a) $this->args[$key] = $a; } public function __get($var) { if(isset($this->$var)) return $this->args[$var]; else { $vars = array_keys(get_object_vars($this)); if(in_array($var, $vars)) { return $this->$var; } } return null; } //if you want to include an iterator, requires this function. //public function getIterator() { // return new iterator($this->args); //} protected function __unset($var) { if(isset($this->args[$var])) unset($this->args[$var]); } } /* END BASE CLASS */ /* NVP MAIN CLASS */ class nvp { private $ch = null; private function initCurl() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, PP_API_ENDPOINT); //switch on for debugging //curl_setopt($ch, CURLOPT_VERBOSE, 1); //turning on the server and trust verification. if you get curl errors, it could be because of your //ssl certificates, set to false (though not v secure) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST, 1); $this->ch = $ch; } public function send(nvpQuery $query) { $this->initCurl(); curl_setopt($this->ch,CURLOPT_POSTFIELDS,$query->queryString); $response = curl_exec($this->ch); if (curl_errno($this->ch)) { //new Exception("Curl Error: " . curl_errno($ch) . ": " . curl_error($ch)); //echo "here!"; $r = new nvpResult('', $query); $r->setErrorMessage("Curl Error: " . curl_errno($ch) . ": " . curl_error($ch)); return $r; } else { curl_close($this->ch); } return new nvpResult($response, $query); } } class nvpQuery extends dataArgumentContainer { public function __construct() { $this->user = PP_API_USERNAME; $this->pwd = PP_API_PASSWORD; $this->version = PP_API_VERSION; $this->signature = PP_API_SIGNATURE; parent::__construct(func_get_args()); } private function convertTime($time) { $time = strtotime($time); $date = date('Y-m-d\T00:00:00\Z', $time); return $date; } public function __get($var) { if($var == "queryString") { //generate the name value pairs, if nvpQuery->queryString is requested return $this->generateQueryString(); } elseif(eregi("date$", $var)) { //convert date automatically for variables with 'date' at the end return $this->convertTime($this->args[$var]); } else { //otherwise goto the default var handler return parent::__get($var); } } public function merge(nvpQuery $query) { if(isset($query->subject)) { $this->subject = $query->subject; } } public function generateQueryString() { $str = ""; foreach($this->args as $var => $arg) { if(strlen($str) > 0) $str .= "&"; $str .= strtoupper($var) . "=" . urlencode($this->$var); } //echo $str; return $str; } } class nvpResult extends dataArgumentContainer { public $rows = array(); private $fault = false; public $nvpquery = null; //private $nvp = null; public function __construct($response, nvpQuery $query) { parent::__construct(); $this->nvpquery = $query; $this->decodeResponse($response); } public function updateTransactions($nvp) { //update your transactions into your database. } public function setErrorMessage($msg) { $this->l_longmessage0 = $msg; } public function isSuccess() { return eregi("^Success", $this->ack) && !$this->fault; } public function __get($var) { if($var == "errorMessage") { return $this->l_longmessage0; } return parent::__get($var); } public function getTransactionsDetails($nvp) { foreach($this->rows as &$tr) { //do something with your transaction ($tr) here. } } private function extractList($class) { foreach($this->args as $key => $value) { if(preg_match("/^l_(.*?)([0-9]+)$/", $key, $c)) { if(!isset($this->rows[$c[2]])) $this->rows[$c[2]] = new $class($this->nvpquery); $row = &$this->rows[$c[2]]; $row->{$c[1]} = $value; //get rid of the variable from the the current args list. unset($this->{$c[0]}); } } } public function count() { return count($this->rows); } private function orderList() { $dates = array(); foreach($this->rows as $obj) { $dates[]=strtotime($obj->ordertime); } array_multisort($dates, SORT_NUMERIC, SORT_ASC, $this->rows); } private function decodeResponse($nvpstr) { $init=0; if(strlen($nvpstr) == 0) $this->fault = true; while(strlen($nvpstr)){ //postion of Key $keypos= strpos($nvpstr,'='); //position of value $valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr); /*getting the Key and Value values and storing in a Associative Array*/ $keyval=substr($nvpstr,$init,$keypos); $valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1); //decoding the respose $val = strtolower(urldecode($keyval)); $this->$val = urldecode($valval); $nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr)); } if($this->isSuccess()) { //change the list depending on what you need switch($this->nvpquery->method) { case "TransactionSearch": //like a slightly improved version of stdClass. $this->extractList("nvpItem"); //order by date ascending $this->orderList(); break; case "AnotherPayPalMethod": //do stuff. break; default: echo "Method not recognised"; } } } } class nvpItem extends dataArgumentContainer { private $nvpquery = null; public function __construct(nvpQuery &$query) { $this->nvpquery = $query; parent::__construct(); } } ## Example Usage /*$nvp = new nvp(); $result = $nvp->send(new nvpQuery("method", "TransactionSearch", "subject", "theotherperson'spaypalemailaddress", "startdate", "10th December 2008", "enddate", "11th December 2008")); if($result->isSuccess()) { $result->getTransactionsDetails($nvp); echo "
";
	print_r($result);
	echo "
"; } else { echo "
";
	echo print_r($result->nvpquery);
	echo "
"; echo "ERROR: $result->errorMessage
"; }*/ ?>