Created
August 24, 2023 06:22
-
-
Save ykurnia/6d0024a38d491c47dd6b371a12e35183 to your computer and use it in GitHub Desktop.
example rest api model to salesforce integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
use Yii; | |
/** | |
* CLASS AS WRAPPER FOR SF REST API functions | |
* | |
* @author Yudi K. | |
*/ | |
class SalesforceRestModel extends yii\base\BaseObject | |
{ | |
public $config = [ | |
'rest_login' => '', | |
'rest_endpoint' => '', | |
'client_id' => '', | |
'client_secret' => '', | |
'username' => '', | |
'password' => '', | |
'token' => '', | |
]; | |
public $curl; | |
public $access_token; | |
/* | |
* constructor | |
*/ | |
public function init($options = []) | |
{ | |
parent::init(); | |
$this->config = Yii::$app->params; | |
if (is_array($options)) { | |
if (isset($options['curl'])) { | |
$this->curl = $options['curl']; | |
} | |
} | |
} | |
/** | |
* Login and get access token | |
* | |
* @return Boolean | |
*/ | |
public function getAccessToken() | |
{ | |
$this->access_token = ''; // default | |
curl_setopt_array($this->curl, array( | |
CURLOPT_URL => $this->config['rest_login'], | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => array( | |
'grant_type' => 'password', | |
'client_id' => $this->config['client_id'], | |
'client_secret' => $this->config['client_secret'], | |
'username' => $this->config['username'], | |
'password' => $this->config['password'].$this->config['token'], | |
), | |
)); | |
$response = curl_exec($this->curl); | |
$arr = ($response == null) ? [] : json_decode($response, true); | |
if (isset($arr['access_token'])) { | |
$this->access_token = $arr['access_token']; | |
} | |
return ($this->access_token != ''); | |
} | |
/** | |
* Function to insert data to an object | |
* Assumption data are valid | |
* @param String $object Object name | |
* @param Array $data | |
* @return Array Respon form server | |
*/ | |
public function insert($object, $data) | |
{ | |
$arrResult = []; | |
$url = $this->config['rest_endpoint']."/sobjects/".$object; | |
$jsonData = json_encode($data); | |
Yii::info('INSERT DATA '.$object); | |
Yii::info($jsonData, __METHOD__); | |
/** @todo check if access_token still valid */ | |
curl_setopt_array($this->curl, array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => $jsonData, | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer ".$this->access_token, | |
"Content-Type: application/json", | |
), | |
)); | |
$response = curl_exec($this->curl); | |
Yii::info($response, __METHOD__); | |
$arrResult = ($response == null) ? [] : json_decode($response, true); | |
return $arrResult; | |
} | |
/** | |
* Function to update data to an object | |
* Assumption data are valid | |
* @param String $object Object name | |
* @param String $id ID of record | |
* @param Array $data | |
* @return Integer | Array Respon form server | |
*/ | |
public function update($object, $id, $data) | |
{ | |
$arrResult = []; | |
$url = $this->config['rest_endpoint']."/sobjects/".$object."/".$id; | |
$jsonData = json_encode($data); | |
/** @todo check if access_token still valid */ | |
Yii::info('UPDATE DATA '.$object." - ".$id); | |
Yii::info($jsonData, __METHOD__); | |
curl_setopt_array($this->curl, array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "PATCH", | |
CURLOPT_POSTFIELDS => $jsonData, | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer ".$this->access_token, | |
"Content-Type: application/json" | |
), | |
)); | |
$arrResult = curl_exec($this->curl); | |
Yii::info($arrResult, __METHOD__); | |
return $arrResult; | |
} | |
/** | |
* Function to load data with query string | |
*/ | |
public function query($str) | |
{ | |
$arrResult = []; | |
$str = urlencode($str); | |
$url = $this->config['rest_endpoint']."/query?q=".$str; | |
curl_setopt_array($this->curl, array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer ".$this->access_token | |
), | |
)); | |
$response = curl_exec($this->curl); | |
$arrResult = json_decode($response,true); | |
if (isset($arrResult['records'])) $arrResult = $arrResult['records']; // only get the records | |
if (isset($arrResult[0]['errorCode'])) { | |
Yii::error('QUERY ERROR '.$arrResult[0]['errorCode']); | |
} | |
return $arrResult; | |
} | |
/** | |
* Function to find record by Id | |
*/ | |
public function findById($object, $id, $fields) | |
{ | |
if ($object != '' && $id != '' && $fields != '') | |
{ | |
$strQuery = "SELECT $fields FROM $object WHERE id = '$id' "; | |
return $this->query($strQuery); | |
} | |
return null; | |
} | |
/** | |
* Function to get object describe | |
*/ | |
public function getDescribe($object) | |
{ | |
$arrResult = []; | |
$url = $this->config['rest_endpoint']."/sobjects/".$object."/describe"; | |
curl_setopt_array($this->curl, array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer ".$this->access_token | |
), | |
)); | |
$response = curl_exec($this->curl); | |
$arrResult = json_decode($response,true); | |
return $arrResult; | |
} | |
/** | |
* Function to get picklist value from object | |
* @param String $object name of object | |
* @param String $field name of field | |
* @return Array List of picklist options | |
*/ | |
public function getPicklist($object, $field) | |
{ | |
$arrResult = []; | |
$arr = $this->getDescribe($object); | |
if (is_array($arr) && isset($arr['fields'])) | |
{ | |
foreach($arr['fields'] AS $i => $fields) | |
{ | |
if ($fields['name'] == $field) | |
{ | |
if (isset($fields['picklistValues'])) | |
{ | |
foreach($fields['picklistValues'] AS $j => $picklist) | |
{ | |
$arrResult[$picklist['value']] = $picklist['label']; | |
} | |
} | |
break; | |
} | |
} | |
} | |
return $arrResult; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment