| 1 |
<?php
|
| 2 |
|
| 3 |
/*
|
| 4 |
|
| 5 |
salesforce.com Partner PHP client
|
| 6 |
|
| 7 |
Copyright (c) 2005 Ryan Choi
|
| 8 |
|
| 9 |
This library is free software; you can redistribute it and/or
|
| 10 |
modify it under the terms of the GNU Lesser General Public
|
| 11 |
License as published by the Free Software Foundation; either
|
| 12 |
version 2.1 of the License, or (at your option) any later version.
|
| 13 |
|
| 14 |
This library is distributed in the hope that it will be useful,
|
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 17 |
Lesser General Public License for more details.
|
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public
|
| 20 |
License along with this library; if not, write to the Free Software
|
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 22 |
|
| 23 |
If you have any questions or comments, please email:
|
| 24 |
|
| 25 |
Ryan Choi
|
| 26 |
rchoi21@hotmail.com
|
| 27 |
http://www.ryankicks.com
|
| 28 |
|
| 29 |
*/
|
| 30 |
|
| 31 |
/**
|
| 32 |
* salesforce client for use with a modified nuSOAP PHP library.
|
| 33 |
*/
|
| 34 |
|
| 35 |
require_once('nusoap.php');
|
| 36 |
|
| 37 |
/**
|
| 38 |
*
|
| 39 |
* salesforce
|
| 40 |
*
|
| 41 |
* @author Ryan Choi <rchoi21@hotmail.com>
|
| 42 |
* @version
|
| 43 |
* @access public
|
| 44 |
*/
|
| 45 |
class salesforce {
|
| 46 |
|
| 47 |
var $partnerNs = 'urn:partner.soap.sforce.com';
|
| 48 |
|
| 49 |
var $client;
|
| 50 |
var $result;
|
| 51 |
|
| 52 |
var $url;
|
| 53 |
var $session;
|
| 54 |
|
| 55 |
/**
|
| 56 |
* constructor for salesforce client.
|
| 57 |
*
|
| 58 |
* @param string
|
| 59 |
* $wsdl local reference to partner WSDL file.
|
| 60 |
* @access public
|
| 61 |
*/
|
| 62 |
function salesforce($wsdl) {
|
| 63 |
|
| 64 |
$this->client = new nusoapclient($wsdl, true);
|
| 65 |
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* login with username and password. will set SessionId header upon
|
| 70 |
* successful login.
|
| 71 |
*
|
| 72 |
* @param string
|
| 73 |
* $username username for login.
|
| 74 |
* @param string
|
| 75 |
* $password password for login.
|
| 76 |
* @return mixed LoginResult complex type. (See WSDL.)
|
| 77 |
* @access public
|
| 78 |
*/
|
| 79 |
function login($username, $password){
|
| 80 |
|
| 81 |
// Doc/lit parameters get wrapped
|
| 82 |
$param = array('username' => $username, 'password' => $password);
|
| 83 |
$this->result = $this->client->call('login', array('parameters' => $param), '', '', false, true);
|
| 84 |
|
| 85 |
if ($this->client->getError() || $this->client->fault) {
|
| 86 |
|
| 87 |
return false;
|
| 88 |
|
| 89 |
} else {
|
| 90 |
|
| 91 |
$wrapper = $this->result['result'];
|
| 92 |
$url = $wrapper['serverUrl'];
|
| 93 |
$session = $wrapper['sessionId'];
|
| 94 |
|
| 95 |
// setup the client's URL with the response from the login
|
| 96 |
$this->setURL($url);
|
| 97 |
|
| 98 |
// setup the client's session with the response from the login
|
| 99 |
$this->setSessionId($session);
|
| 100 |
|
| 101 |
return $this->result['result'];
|
| 102 |
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* set session of client. called in login, and accessible for manual
|
| 108 |
* setting if session already available.
|
| 109 |
*
|
| 110 |
* @param string
|
| 111 |
* $session session string
|
| 112 |
* @return none
|
| 113 |
* @access public
|
| 114 |
*/
|
| 115 |
function setSessionId($session){
|
| 116 |
|
| 117 |
$element = new soapval('sessionId', null, $session);
|
| 118 |
$element = array($element);
|
| 119 |
$this->setHeader('SessionHeader', $element);
|
| 120 |
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* set URL of client. all requests are done insecure.
|
| 125 |
*
|
| 126 |
* @param string
|
| 127 |
* $url URL of API.
|
| 128 |
* @return none
|
| 129 |
* @access public
|
| 130 |
*/
|
| 131 |
function setURL($url){
|
| 132 |
$this->client->forceEndpoint = str_replace("https", "http", $url);
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* set header on client.
|
| 137 |
*
|
| 138 |
* @param string
|
| 139 |
* $headerName name of header
|
| 140 |
* @param array
|
| 141 |
* $headerValue array of soapvals of values
|
| 142 |
* @access public
|
| 143 |
*/
|
| 144 |
function setHeader($headerName, $headerValue){
|
| 145 |
|
| 146 |
$header = new soapval($headerName, null, $headerValue, $this->partnerNs);
|
| 147 |
$headers = null;
|
| 148 |
|
| 149 |
if ($this->client->requestHeaders == null){
|
| 150 |
$headers = array($header);
|
| 151 |
} else {
|
| 152 |
$headers = $this->client->requestHeaders;
|
| 153 |
$count = 0;
|
| 154 |
foreach ($headers as $hdr) {
|
| 155 |
$existingHdrName = $hdr->name;
|
| 156 |
if ($existingHdrName == $headerName){
|
| 157 |
break;
|
| 158 |
}
|
| 159 |
$count++;
|
| 160 |
}
|
| 161 |
array_splice($headers, $count, 1, array($header));
|
| 162 |
}
|
| 163 |
|
| 164 |
$this->client->setHeaders($headers);
|
| 165 |
|
| 166 |
}
|
| 167 |
|
| 168 |
/**
|
| 169 |
* create sObjects.
|
| 170 |
*
|
| 171 |
* @param mixed
|
| 172 |
* $values either a single sObject or an array of sObjects
|
| 173 |
* @return mixed either a single SaveResult complex type or an array of
|
| 174 |
* SaveResult complex types. (See WSDL.)
|
| 175 |
* @access public
|
| 176 |
*/
|
| 177 |
function create($sObjects){
|
| 178 |
$param = array('sObjects' => $sObjects);
|
| 179 |
$this->result = $this->client->call('create', array('parameters' => $param), '', '', false, true);
|
| 180 |
return $this->result['result'];
|
| 181 |
}
|
| 182 |
|
| 183 |
/**
|
| 184 |
* update sObjects.
|
| 185 |
*
|
| 186 |
* @param mixed
|
| 187 |
* $values either a single sObject or an array of sObjects
|
| 188 |
* @return mixed either a single SaveResult complex type or an array of
|
| 189 |
* SaveResult complex types. (See WSDL.)
|
| 190 |
* @access public
|
| 191 |
*/
|
| 192 |
function update($sObjects){
|
| 193 |
$param = array('sObjects' => $sObjects);
|
| 194 |
$this->result = $this->client->call('update', array('parameters' => $param), '', '', false, true);
|
| 195 |
return $this->result['result'];
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* delete sObjects.
|
| 200 |
*
|
| 201 |
* @param mixed
|
| 202 |
* $values either a single id (string) or an array of ids (array
|
| 203 |
* of strings)
|
| 204 |
* @return mixed either a single DeleteResult complex type or an array of
|
| 205 |
* DeleteResult complex types. (See WSDL.)
|
| 206 |
* @access public
|
| 207 |
*/
|
| 208 |
function delete($ids){
|
| 209 |
$param = array('ids' => $ids);
|
| 210 |
$this->result = $this->client->call('delete', array('parameters' => $param), '', '', false, true);
|
| 211 |
return $this->result['result'];
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* perform query using SOQL command.
|
| 216 |
*
|
| 217 |
* @param string
|
| 218 |
* $queryString SOQL query request
|
| 219 |
* @return mixed QueryResult complex type. (See WSDL.)
|
| 220 |
* @access public
|
| 221 |
*/
|
| 222 |
function query($queryString){
|
| 223 |
$param = array('queryString' => $queryString);
|
| 224 |
$this->result = $this->client->call('query', array('parameters' => $param), '', '', false, true);
|
| 225 |
return $this->result['result'];
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
* queries more query records based on query locator.
|
| 230 |
*
|
| 231 |
* @param string
|
| 232 |
* $queryLocator string representing query locator
|
| 233 |
* @return mixed QueryResult complex type. (See WSDL.)
|
| 234 |
* @access public
|
| 235 |
*/
|
| 236 |
function queryMore($queryLocator){
|
| 237 |
$param = array('queryLocator' => $queryLocator);
|
| 238 |
$this->result = $this->client->call('queryMore', array('parameters' => $param), '', '', false, true);
|
| 239 |
return $this->result['result'];
|
| 240 |
}
|
| 241 |
|
| 242 |
/**
|
| 243 |
* perform retrieve
|
| 244 |
*
|
| 245 |
* @param string
|
| 246 |
* $fieldList fields of sObject to return
|
| 247 |
* $sObjectType type of object to retrieve
|
| 248 |
* $ids ids of sObjects to retrieve
|
| 249 |
* @return mixed array of sObjects. (See WSDL.)
|
| 250 |
* @access public
|
| 251 |
*/
|
| 252 |
function retrieve($fieldList, $sObjectType, $ids){
|
| 253 |
$param = array(
|
| 254 |
'fieldList' => $fieldList,
|
| 255 |
'sObjectType' => $sObjectType,
|
| 256 |
'ids' => $ids
|
| 257 |
);
|
| 258 |
$this->result = $this->client->call('retrieve', array('parameters' => $param), '', '', false, true);
|
| 259 |
return $this->result['result'];
|
| 260 |
}
|
| 261 |
|
| 262 |
/**
|
| 263 |
* get sObjects updated during a specified interval
|
| 264 |
*
|
| 265 |
* @param string
|
| 266 |
* $sObjectType type of sObject
|
| 267 |
* $startDate
|
| 268 |
* $endDate
|
| 269 |
* @return mixed GetUpdatedResult complex type. (See WSDL.)
|
| 270 |
* @access public
|
| 271 |
*/
|
| 272 |
function getUpdated($sObjectType, $startDate, $endDate){
|
| 273 |
$param = array(
|
| 274 |
'sObjectType' => $sObjectType,
|
| 275 |
'startDate' => $startDate,
|
| 276 |
'endDate' => $endDate
|
| 277 |
);
|
| 278 |
$this->result = $this->client->call('getUpdated', array('parameters' => $param), '', '', false, true);
|
| 279 |
return $this->result['result'];
|
| 280 |
}
|
| 281 |
|
| 282 |
/**
|
| 283 |
* get ids of sObjects deleted during a specified interval
|
| 284 |
*
|
| 285 |
* @param string
|
| 286 |
* $sObjectType type of sObject
|
| 287 |
* $startDate
|
| 288 |
* $endDate
|
| 289 |
* @return mixed GetDeletedResult complex type. (See WSDL.)
|
| 290 |
* @access public
|
| 291 |
*/
|
| 292 |
function getDeleted($sObjectType, $startDate, $endDate){
|
| 293 |
$param = array(
|
| 294 |
'sObjectType' => $sObjectType,
|
| 295 |
'startDate' => $startDate,
|
| 296 |
'endDate' => $endDate
|
| 297 |
);
|
| 298 |
$this->result = $this->client->call('getDeleted', array('parameters' => $param), '', '', false, true);
|
| 299 |
return $this->result['result'];
|
| 300 |
}
|
| 301 |
|
| 302 |
|
| 303 |
/**
|
| 304 |
* perform lead converts.
|
| 305 |
*
|
| 306 |
* @param mixed $leadConverts either a single LeadConvert object or an array of LeadConvert objects
|
| 307 |
* @return mixed LeadConverResult complex types. (See WSDL.)
|
| 308 |
* @access public
|
| 309 |
*/
|
| 310 |
function convertLead($leadConverts){
|
| 311 |
$param = array('leadConverts' => $leadConverts);
|
| 312 |
$this->result = $this->client->call('convertLead', array('parameters' => $param), '', '', false, true);
|
| 313 |
return $this->result['result'];
|
| 314 |
}
|
| 315 |
|
| 316 |
/**
|
| 317 |
* returns current time of salesforce server
|
| 318 |
*
|
| 319 |
* @return timestamp time at sforce server.
|
| 320 |
* @access public
|
| 321 |
*/
|
| 322 |
function getServerTimestamp(){
|
| 323 |
$param = array('' => '');
|
| 324 |
$this->result = $this->client->call('getServerTimestamp', array('parameters' => $param), '', '', false, true);
|
| 325 |
return $this->result['result']['timestamp'];
|
| 326 |
}
|
| 327 |
|
| 328 |
/**
|
| 329 |
* returns global description of API settings
|
| 330 |
*
|
| 331 |
* @return mixed DescribeGlobalResult complex type. (See WSDL.)
|
| 332 |
* @access public
|
| 333 |
*/
|
| 334 |
function describeGlobal(){
|
| 335 |
$param = array('' => '');
|
| 336 |
$this->result = $this->client->call('describeGlobal', array('parameters' => $param), '', '', false, true);
|
| 337 |
return $this->result['result'];
|
| 338 |
}
|
| 339 |
|
| 340 |
/**
|
| 341 |
* returns description for given sObject
|
| 342 |
*
|
| 343 |
* @param string
|
| 344 |
* $sObjectType string sObject name
|
| 345 |
* @return mixed DescribeSObjectResult complex type. (See WSDL.)
|
| 346 |
* @access public
|
| 347 |
*/
|
| 348 |
function describeSObject($sObjectType){
|
| 349 |
$param = array('sObjectType' => $sObjectType);
|
| 350 |
$this->result = $this->client->call('describeSObject', array('parameters' => $param), '', '', false, true);
|
| 351 |
return $this->result['result'];
|
| 352 |
}
|
| 353 |
|
| 354 |
/**
|
| 355 |
* returns describe results for multiple sObjects.
|
| 356 |
*
|
| 357 |
* @param array
|
| 358 |
* $sObjectTypes array of string sObject names
|
| 359 |
* @return array array of DescribeSObjectResult complex types. (See WSDL.)
|
| 360 |
* @access public
|
| 361 |
*/
|
| 362 |
function describeSObjects($sObjectTypes){
|
| 363 |
$param = array('sObjectType' => $sObjectTypes);
|
| 364 |
$this->result = $this->client->call('describeSObjects', array('parameters' => $param), '', '', false, true);
|
| 365 |
return $this->result['result'];
|
| 366 |
}
|
| 367 |
|
| 368 |
/**
|
| 369 |
* returns layouts for given sObject
|
| 370 |
*
|
| 371 |
* @param string
|
| 372 |
* $sObjectType string sObject name
|
| 373 |
* @return mixed DescribeLayoutResult complex type. (See WSDL.)
|
| 374 |
* @access public
|
| 375 |
*/
|
| 376 |
function describeLayout($sObjectType){
|
| 377 |
$param = array('sObjectType' => $sObjectType);
|
| 378 |
$this->result = $this->client->call('describeLayout', array('parameters' => $param), '', '', false, true);
|
| 379 |
return $this->result['result'];
|
| 380 |
}
|
| 381 |
|
| 382 |
/**
|
| 383 |
* searches for specified entities given SOSL
|
| 384 |
*
|
| 385 |
* @param string
|
| 386 |
* $searchString SOSL search request
|
| 387 |
* @return mixed SearchResult complex type. (See WSDL.)
|
| 388 |
* @access public
|
| 389 |
*/
|
| 390 |
function search($searchString){
|
| 391 |
$param = array('searchString' => $searchString);
|
| 392 |
$this->result = $this->client->call('search', array('parameters' => $param), '', '', false, true);
|
| 393 |
return $this->result['result'];
|
| 394 |
}
|
| 395 |
|
| 396 |
}
|
| 397 |
|
| 398 |
/**
|
| 399 |
* salesforce sObject.
|
| 400 |
*
|
| 401 |
* @access public
|
| 402 |
*/
|
| 403 |
class sObject {
|
| 404 |
|
| 405 |
var $type;
|
| 406 |
var $id;
|
| 407 |
var $values;
|
| 408 |
var $fieldsToNull;
|
| 409 |
|
| 410 |
function sObject($type, $id=null, $values=null, $fieldsToNull=null) {
|
| 411 |
|
| 412 |
// deserialize record from nusoap.php
|
| 413 |
if (is_array($type)){
|
| 414 |
|
| 415 |
$this->values = array();
|
| 416 |
|
| 417 |
foreach ($type as $k => $v){
|
| 418 |
if ($k == 'type'){
|
| 419 |
$this->type = $v;
|
| 420 |
} else if ($k == 'Id'){
|
| 421 |
if (is_array($v)){
|
| 422 |
$this->id = $v[0];
|
| 423 |
} else {
|
| 424 |
$this->id = $v;
|
| 425 |
}
|
| 426 |
} else {
|
| 427 |
$this->values[$k] = $v;
|
| 428 |
}
|
| 429 |
}
|
| 430 |
|
| 431 |
} else {
|
| 432 |
|
| 433 |
$this->type = $type;
|
| 434 |
$this->id = $id;
|
| 435 |
$this->values = $values;
|
| 436 |
$this->fieldsToNull = $fieldsToNull;
|
| 437 |
|
| 438 |
}
|
| 439 |
}
|
| 440 |
|
| 441 |
function serialize(){
|
| 442 |
|
| 443 |
$valuesSer['type'] = $this->type;
|
| 444 |
if ($this->fieldsToNull != null){
|
| 445 |
$fieldsToNull = array();
|
| 446 |
$index = 0;
|
| 447 |
foreach($this->fieldsToNull as $value){
|
| 448 |
$fieldsToNull[$index] = $value;
|
| 449 |
$index++;
|
| 450 |
}
|
| 451 |
$valuesSer['fieldsToNull'] = new RepeatedElementsArray('fieldsToNull', $fieldsToNull);
|
| 452 |
}
|
| 453 |
$valuesSer['Id'] = $this->id;
|
| 454 |
|
| 455 |
foreach ($this->values as $k => $v) {
|
| 456 |
$valuesSer[$k] = $v;
|
| 457 |
}
|
| 458 |
|
| 459 |
$sobj = new soapval('sObject', false, $valuesSer);
|
| 460 |
|
| 461 |
return $sobj ->serialize();
|
| 462 |
|
| 463 |
}
|
| 464 |
|
| 465 |
}
|
| 466 |
|
| 467 |
/**
|
| 468 |
* salesforce sObject.
|
| 469 |
*
|
| 470 |
* @access public
|
| 471 |
*/
|
| 472 |
class LeadConvert {
|
| 473 |
|
| 474 |
var $accountId;
|
| 475 |
var $contactId;
|
| 476 |
var $convertedStatus;
|
| 477 |
var $doNotCreateOpportunity;
|
| 478 |
var $leadId;
|
| 479 |
var $opportunityName;
|
| 480 |
var $overwriteLeadSource;
|
| 481 |
var $ownerId;
|
| 482 |
var $sendNotificationEmail;
|
| 483 |
|
| 484 |
function LeadConvert($accountId, $contactId, $convertedStatus, $doNotCreateOpportunity, $leadId, $opportunityName, $overwriteLeadSource, $ownerId, $sendNotificationEmail){
|
| 485 |
|
| 486 |
$this->accountId = $accountId;
|
| 487 |
$this->contactId = $contactId;
|
| 488 |
$this->convertedStatus = $convertedStatus;
|
| 489 |
$this->doNotCreateOpportunity = $doNotCreateOpportunity;
|
| 490 |
$this->leadId = $leadId;
|
| 491 |
$this->opportunityName = $opportunityName;
|
| 492 |
$this->overwriteLeadSource = $overwriteLeadSource;
|
| 493 |
$this->ownerId = $ownerId;
|
| 494 |
$this->sendNotificationEmail = $sendNotificationEmail;
|
| 495 |
|
| 496 |
}
|
| 497 |
|
| 498 |
function serialize(){
|
| 499 |
|
| 500 |
$valuesSer['accountId'] = $this->accountId;
|
| 501 |
$valuesSer['contactId'] = $this->contactId;
|
| 502 |
$valuesSer['convertedStatus'] = $this->convertedStatus;
|
| 503 |
$valuesSer['doNotCreateOpportunity'] = $this->doNotCreateOpportunity;
|
| 504 |
$valuesSer['leadId'] = $this->leadId;
|
| 505 |
$valuesSer['opportunityName'] = $this->opportunityName;
|
| 506 |
$valuesSer['overwriteLeadSource'] = $this->overwriteLeadSource;
|
| 507 |
$valuesSer['ownerId'] = $this->ownerId;
|
| 508 |
$valuesSer['sendNotificationEmail'] = $this->sendNotificationEmail;
|
| 509 |
|
| 510 |
$leadConvert = new soapval('LeadConvert', false, $valuesSer);
|
| 511 |
return $leadConvert->serialize();
|
| 512 |
|
| 513 |
}
|
| 514 |
|
| 515 |
|
| 516 |
}
|
| 517 |
|
| 518 |
|
| 519 |
/**
|
| 520 |
* helps SOAP-ENC arrays to be encoded as repeated elements.
|
| 521 |
*
|
| 522 |
* @access private
|
| 523 |
*/
|
| 524 |
class RepeatedElementsArray {
|
| 525 |
|
| 526 |
var $elementName;
|
| 527 |
var $values;
|
| 528 |
|
| 529 |
/**
|
| 530 |
* @param string
|
| 531 |
* name of element
|
| 532 |
* @param array
|
| 533 |
* values to be encoded. Currently, only strings are supported.
|
| 534 |
* @access public
|
| 535 |
*/
|
| 536 |
function RepeatedElementsArray($elementName, $values){
|
| 537 |
$this->elementName = $elementName;
|
| 538 |
$this->values = $values;
|
| 539 |
}
|
| 540 |
|
| 541 |
function serialize($use='encoded'){
|
| 542 |
$xml = "";
|
| 543 |
foreach($this->values as $value){
|
| 544 |
$xml .= "<$this->elementName>$value</$this->elementName>";
|
| 545 |
}
|
| 546 |
return $xml;
|
| 547 |
}
|
| 548 |
|
| 549 |
}
|