>./ Alex Tech Blog / Using PHP to query SalesForce

Using PHP to query SalesForce

#php #salesforce

Quick gist showing how to use PHP and PHP-Curl to query Salesforce data

 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
<?php

$sf_config = array(
	client_id     => '', // from salesforce OAuth configuration (add new app ...)
	client_secret => '',
	username      => '', // username login to salesforce
	password      => '', // no need to concat password token
	url           => 'https://login.salesforce.com/services/oauth2/token',
	grant_type    => 'password',
	query_srv_url => '/services/data/v20.0/query/'
);

$soql_query = 'SELECT id, name from Account';

// ---------
// first act - get token from salesforce

$curl_post_param  = 'grant_type='. $sf_config[grant_type];
$curl_post_param .= '&client_id='. $sf_config[client_id];
$curl_post_param .= '&client_secret='. $sf_config[client_secret];
$curl_post_param .= '&username='. $sf_config[username];
$curl_post_param .= '&password='. $sf_config[password];

$curl_getToken = curl_init();

curl_setopt_array($curl_getToken, array(
	CURLOPT_URL            => $sf_config[url],
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING       => "",
	CURLOPT_MAXREDIRS      => 10,
	CURLOPT_TIMEOUT        => 30,
	CURLOPT_SSL_VERIFYPEER => false,
	CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST  => "POST",
	CURLOPT_POSTFIELDS     => $curl_post_param,
	CURLOPT_HTTPHEADER     => array("content-type: application/x-www-form-urlencoded"),
));

$response = curl_exec($curl_getToken);
$err      = curl_error($curl_getToken);

curl_close($curl_getToken);

if ($err)
{
	echo "cURL Error #:" . $err;
	die();
};

$sf_token = json_decode($response, true);

echo "Salesforce token: \n";
var_dump($sf_token);

// ----------
// Second act - send a soql query to salesforce

$curl_url = $sf_token[instance_url] . $sf_config[query_srv_url] . '?q=' .urlencode($soql_query);

$curl_getSoql = curl_init();
curl_setopt_array($curl_getSoql, array(
	CURLOPT_URL            => $curl_url,
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING       => "",
	CURLOPT_MAXREDIRS      => 10,
	CURLOPT_TIMEOUT        => 30,
	CURLOPT_SSL_VERIFYPEER => false,
	CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST  => "GET",
	CURLOPT_HTTPHEADER     => array('Authorization: '. $sf_token[token_type] .' '. $sf_token[access_token]),
));

$response = curl_exec($curl_getSoql);
$err      = curl_error($curl_getSoql);

curl_close($curl_getSoql);

if ($err)
{
	echo "cURL Error #:" . $err;
	die();
};

$sf_soql_reply = json_decode($response, true);
echo "Salesforce SOQL reply: \n";
var_dump($sf_soql_reply);
#aws #cheat sheet #codecommit #esri #github #honeycode #javascript #jupyter #linux #machine learning #pandas #php #python #salesforce #ssh