-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClient.php
More file actions
102 lines (90 loc) · 2.25 KB
/
Client.php
File metadata and controls
102 lines (90 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Phpforce\RestClient;
use Phpforce\SoapClient\ClientInterface;
use Guzzle\Http\Client as GuzzleClient;
use Phpforce\SoapClient\Result\LoginResult;
/**
* A client for the Salesforce REST API
*/
class Client
{
/**
* SOAP client
*
* @var ClientInterface
*/
protected $soapClient;
/**
* Constructor
*
* @param ClientInterface $soapClient
*/
public function __construct(ClientInterface $soapClient)
{
$this->soapClient = $soapClient;
}
/**
* Perform call to Salesforce REST API
*
* @param string $method
* @param array $arguments
*
* @return array The JSON response as an array
*/
public function call($method, $arguments = null)
{
$request = $this->getClient()->get($method);
$request->setHeader('Authorization', 'Bearer ' . $this->getSessionId());
$request->getQuery()->merge($arguments);
$response = $request->send();
return $response->json();
}
/**
* Perform post to Salesforce REST API
*
* @param string $uri
* @param array $arguments
*
* @return array The JSON response as an array
*/
public function post($uri, array $arguments = array())
{
$request = $this->getClient()->post($uri);
$request->setHeader('Authorization', 'Bearer ' . $this->getSessionId());
$request->setBody(json_encode($arguments), 'application/json');
$response = $request->send();
return $response->json();
}
/**
* Get Guzzle client
*
* @return \Guzzle\Http\Client
*/
protected function getClient()
{
return new GuzzleClient(
sprintf(
'https://%s.salesforce.com/services/apexrest',
$this->getLoginResult()->getServerInstance()
)
);
}
/**
* Get login result from SOAP client
*
* @return LoginResult
*/
protected function getLoginResult()
{
return $this->soapClient->getLoginResult();
}
/**
* Get current session id through Salesforce SOAP API
*
* @return string
*/
protected function getSessionId()
{
return $this->getLoginResult()->getSessionId();
}
}