Subversion Repositories munaweb

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
<?php
2
/*  © 2013 eBay Inc., All Rights Reserved */
3
/* Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php */
4
 
5
class eBaySession
6
{
7
	private $requestToken;
8
	private $devID;
9
	private $appID;
10
	private $certID;
11
	private $serverUrl;
12
	private $compatLevel;
13
	private $siteID;
14
	private $verb;
15
 
16
	/**	__construct
17
		Constructor to make a new instance of eBaySession with the details needed to make a call
18
		Input:	$userRequestToken - the authentication token fir the user making the call
19
				$developerID - Developer key obtained when registered at http://developer.ebay.com
20
				$applicationID - Application key obtained when registered at http://developer.ebay.com
21
				$certificateID - Certificate key obtained when registered at http://developer.ebay.com
22
				$useTestServer - Boolean, if true then Sandbox server is used, otherwise production server is used
23
				$compatabilityLevel - API version this is compatable with
24
				$siteToUseID - the Id of the eBay site to associate the call iwht (0 = US, 2 = Canada, 3 = UK, ...)
25
				$callName  - The name of the call being made (e.g. 'GeteBayOfficialTime')
26
		Output:	Response string returned by the server
27
	*/
28
	public function __construct($userRequestToken, $developerID, $applicationID, $certificateID, $serverUrl,
29
								$compatabilityLevel, $siteToUseID, $callName)
30
	{
31
		$this->requestToken = $userRequestToken;
32
		$this->devID = $developerID;
33
		$this->appID = $applicationID;
34
		$this->certID = $certificateID;
35
		$this->compatLevel = $compatabilityLevel;
36
		$this->siteID = $siteToUseID;
37
		$this->verb = $callName;
38
        $this->serverUrl = $serverUrl;
39
	}
40
 
41
 
42
	/**	sendHttpRequest
43
		Sends a HTTP request to the server for this session
44
		Input:	$requestBody
45
		Output:	The HTTP Response as a String
46
	*/
47
	public function sendHttpRequest($requestBody)
48
	{
49
		//build eBay headers using variables passed via constructor
50
		$headers = $this->buildEbayHeaders();
51
 
52
		//initialise a CURL session
53
		$connection = curl_init();
54
		//set the server we are using (could be Sandbox or Production server)
55
		curl_setopt($connection, CURLOPT_URL, $this->serverUrl);
56
 
57
		//stop CURL from verifying the peer's certificate
58
		curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
59
		curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
60
 
61
		//set the headers using the array of headers
62
		curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
63
 
64
		//set method as POST
65
		curl_setopt($connection, CURLOPT_POST, 1);
66
 
67
		//set the XML body of the request
68
		curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody);
69
 
70
		//set it to return the transfer as a string from curl_exec
71
		curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
72
 
73
		//Send the Request
74
		$response = curl_exec($connection);
75
 
76
		//close the connection
77
		curl_close($connection);
78
 
79
		//return the response
80
		return $response;
81
	}
82
 
83
 
84
 
85
	/**	buildEbayHeaders
86
		Generates an array of string to be used as the headers for the HTTP request to eBay
87
		Output:	String Array of Headers applicable for this call
88
	*/
89
	private function buildEbayHeaders()
90
	{
91
		$headers = array (
92
			//Regulates versioning of the XML interface for the API
93
			'X-EBAY-API-COMPATIBILITY-LEVEL: ' . $this->compatLevel,
94
 
95
			//set the keys
96
			'X-EBAY-API-DEV-NAME: ' . $this->devID,
97
			'X-EBAY-API-APP-NAME: ' . $this->appID,
98
			'X-EBAY-API-CERT-NAME: ' . $this->certID,
99
 
100
			//the name of the call we are requesting
101
			'X-EBAY-API-CALL-NAME: ' . $this->verb,
102
 
103
			//SiteID must also be set in the Request's XML
104
			//SiteID = 0  (US) - UK = 3, Canada = 2, Australia = 15, ....
105
			//SiteID Indicates the eBay site to associate the call with
106
			'X-EBAY-API-SITEID: ' . $this->siteID,
107
		);
108
 
109
		return $headers;
110
	}
111
}
112
?>