Skip to content

Instantly share code, notes, and snippets.

@alanzhaonys
Last active November 30, 2020 14:30
Show Gist options
  • Save alanzhaonys/19c965a453554eccaa827ec431e9075b to your computer and use it in GitHub Desktop.
Save alanzhaonys/19c965a453554eccaa827ec431e9075b to your computer and use it in GitHub Desktop.
PHP and Salesforce Integration
<?php
$request = [
'ws_key' => 'SMNXAgMDEA6GyPnYmMheW86AMLfKvqFTDGZR3dsTdcp41o',
];
$sfmc_ws = new SFMCWebService($request, true);
/**
* SFMC web services.
*
* This class defines the web service storing data in SFMC.
*
* @package Azhao.me
* @author Alan Zhao <azhao@azhao.me>
* @version $Revision: 1.0 $
* @access public
* @see http://www.azhao.me/
*/
class SFMCWebService
{
/**
* @var string Web service authentication key
* @access public
*/
const WS_KEY = 'SMNXAgMDEA6GyPnYmMheW86AMLfKvqFTDGZR3dsTdcp41o';
/**
* @var string Salesforce URL to request for access token
* @access public
*/
const SF_TOKEN_URL = 'https://xxxx.auth.marketingcloudapis.com/v2/token';
/**
* @var string REST API URL to search for providers
*/
const REST_API_URL = 'https://xxxx.rest.marketingcloudapis.com';
/**
* @var string Salesforce client ID
* @access public
*/
const SF_CLIENT_ID = 'xxxx';
/**
* @var string Salesforce client secret
* @access public
*/
const SF_CLIENT_SECRET = 'xxxx';
const DEV_KEY = 'xxx-xxx-xxx';
const STAGE_KEY = 'xxx-xxx-xxx';
const PROD_KEY = 'xxx-xxx-xxxx';
/**
* @var array Holds the incoming parameters
* @access private
*/
private $_request = [];
/**
* @var array Holds the parameters to use with REST API
* @access private
*/
private $_rest_params = [];
/**
* @var string Holds Salesforce access token
* @access private
*/
private $_access_token = '';
/**
* Class constructor, defines what to do.
*
* @access public
* @return void
*/
function __construct($request, $debug = false)
{
if ($debug) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
$this->_request = $request;
$this->check_access();
//$this->check_params();
$this->get_sf_access_token();
$this->post_data();
}
/**
* Check if this web service can be accessed.
*
* @access public
* @return void
*/
public function check_access()
{
if (
!isset($this->_request['ws_key'])
|| !$this->_request['ws_key']
|| self::WS_KEY !== $this->_request['ws_key']
) {
$this->access_denied();
}
}
/**
* Check web service parameters.
*
* @access public
* @return void
*/
public function check_params()
{
$required_params = array('address', 'range', 'page', 'items');
foreach ($required_params as $var) {
$var = trim($var);
if (!isset($this->_request[$var]) || $this->_request[$var] == '') {
$this->api_error(strtoupper($var) . ' parameter is required.');
}
$this->_rest_params[$var] = $this->_request[$var];
}
$this->_rest_params['address'] = urlencode($this->_rest_params['address']);
}
/**
* Get Salesforce access token returned from CURL
*
* @access public
* @return void
*/
public function get_sf_access_token()
{
$url = self::SF_TOKEN_URL;
$post_data = array(
'grant_type' => 'client_credentials',
'client_id' => self::SF_CLIENT_ID,
'client_secret' => self::SF_CLIENT_SECRET
);
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Accept: application/json',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$result = curl_exec($ch);
if ($result === false) {
$this->api_error('Error getting access token: '
. curl_error($ch));
}
curl_close($ch);
$json = json_decode($result);
var_dump($json);
if (isset($json->access_token)) {
$this->_access_token = $json->access_token;
}
if (empty($this->_access_token)) {
$this->api_error('Failed to get access token.');
}
}
/**
* Get post data to Salesforce
*
* @access public
* @return void Echoes JSON string
*/
public function post_data()
{
//$url = self::REST_API_URL . '/hub/v1/dataevents/key:' . self::DEV_KEY . '/rows/Release_Email:azhao@azhao.me';
$url = self::REST_API_URL . '/data/v1/async/dataextensions/key:' . self::DEV_KEY . '/rows';
echo $url;
$post_data = array(
'items' => [
[
//'Legal Release_First Name' => 'Alan',
//'Legal Release_Last Name' => 'Zhao',
'Email' => 'azhao@azhao.me',
//'Legal Release_tba button to accept' => 'TRUE',
//'Legal Release_checkbox to subscribe to marketing materials' => 'TRUE',
//'Participant Profile' => 'OR Nurse',
//'Q1_OpenText' => 'Test',
//'Q1_DependOnRoleSelection' => 'test',
//'PR Waiver_Check' => 'test',
//'PR Waiver_Name' => 'Alan Zhao',
//'NPI Number' => '1558444216'
],
[
'Email' => 'azhao@azhao.me',
],
],
);
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $this->_access_token
)
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$result = curl_exec($ch);
var_dump($result);
if ($result === false) {
$this->api_error('Error getting provider results: '
. curl_error($ch));
}
if (empty($result)) {
$this->api_error('API returns no response.');
}
curl_close($ch);
$result = json_decode($result);
$this->verify_result($result->requestId);
$this->verify_status($result->requestId);
echo json_encode($result);
}
/**
* Verify result of Salesforce request
*
* @access public
* @return void Echoes JSON string
*/
public function verify_result($request_id)
{
$url = self::REST_API_URL . '/data/v1/async/' . $request_id . '/results';
echo $url;
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $this->_access_token
)
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$result = curl_exec($ch);
var_dump($result);
if ($result === false) {
$this->api_error('Error getting provider results: '
. curl_error($ch));
}
if (empty($result)) {
$this->api_error('API returns no response.');
}
curl_close($ch);
$result = json_decode($result);
echo json_encode($result);
}
/**
* Verify status of Salesforce request
*
* @access public
* @return void Echoes JSON string
*/
public function verify_status($request_id)
{
$url = self::REST_API_URL . '/data/v1/async/' . $request_id . '/status';
echo $url;
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $this->_access_token
)
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$result = curl_exec($ch);
var_dump($result);
if ($result === false) {
$this->api_error('Error getting provider results: '
. curl_error($ch));
}
if (empty($result)) {
$this->api_error('API returns no response.');
}
curl_close($ch);
$result = json_decode($result);
echo json_encode($result);
}
/**
* No result can be returned.
*
* @access public
* @return void Echoes no result JSON result
*/
public function no_result()
{
echo json_encode(array('error' => false, 'no_result' => true));
exit;
}
/**
* REST API or web service error
*
* @access public
* @param string $message The error message
* @return void Echoes error JSON result
*/
public function api_error($message)
{
echo json_encode(array('error' => true, 'message' => $message));
exit;
}
/**
* Web service access denied.
*
* @access public
* @return void Echoes access denied JSON result
*/
public function access_denied()
{
$this->api_error('Web service access denied.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment