
10/102017
Looking to get your feet wet and start using the Pardot PHP API?
Normally the first place to start when integrating your website with Pardot is the forms. It is much easier to generate a form in Pardot and then just embed the HTML code into your website. But sometimes that is not an option, let’s say for example you have already created your own form that does something more advanced and just need the data to be synced over to your Pardot instance, then using a simple form will not be possible.
The php script below is an easy way to get started with the Pardot API. It uses the sample connection code found at the Pardot official API documentation site; but it goes a little further and shows how to send the data related to your prospect to your Pardot Instance. Of course once you get up and running you might want to create some PHP object oriented code to handle the data sync and your Pardot implementation.
<?php
/**
* Call the Pardot API and get the raw XML response back
*
* @param string $url the full Pardot API URL to call, e.g. "https://pi.pardot.com/api/prospect/version/3/do/query"
* @param array $data the data to send to the API - make sure to include your api_key and user_key for authentication
* @param string $method the HTTP method, one of "GET", "POST", "DELETE"
* @return string the raw XML response from the Pardot API
* @throws Exception if we were unable to contact the Pardot API or something went wrong
*/
function callPardotApi($url, $data, $method = 'GET')
{
// build out the full url, with the query string attached.
$queryString = http_build_query($data, null, '&');
if (strpos($url, '?') !== false) {
$url = $url . '&' . $queryString;
} else {
$url = $url . '?' . $queryString;
}
$curl_handle = curl_init($url);
// wait up to 5 seconds to connect to the Pardot API, and 20 total seconds for everything to complete
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 20);
curl_setopt($curl_handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
// ALWAYS verify SSL - this should only be changed for testing. 2 = strict verify
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
// return the result from the server as the return value of curl_exec instead of echoing it
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
if (strcasecmp($method, 'POST') === 0) {
curl_setopt($curl_handle, CURLOPT_POST, true);
} elseif (strcasecmp($method, 'GET') !== 0) {
// perhaps a DELETE?
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, strtoupper($method));
}
$pardotApiResponse = curl_exec($curl_handle);
if ($pardotApiResponse === false) {
// failure - a timeout or other problem. depending on how you want to handle failures, you may want to modify this code. Some folks might throw an exception here. Some might log the error. May you want to return a value that signifies an error. The choice is yours!
// let's see what went wrong -- first look at curl
$humanReadableError = curl_error($curl_handle);
// you can also get the HTTP response code
$httpResponseCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
// make sure to close your handle before you bug out!
curl_close($curl_handle);
throw new Exception("Unable to successfully complete Pardot API call to $url -- curl error: \"".
"$humanReadableError\", HTTP response code was: $httpResponseCode");
}
// make sure to close your handle!
curl_close($curl_handle);
return $pardotApiResponse;
}
//your API Key (good for 1 hour)
$responsetogetapikey=callPardotApi('https://pi.pardot.com/api/login/version/4', array(
'email' => 'YOURPARDOTEMAIL',
'password' => 'YOURPARDOTPASSWORD',
'user_key' => 'YOURPARDOTKEY' //available from https://pi.pardot.com/account
)
);
$pardotresponsetogetapikey = simplexml_load_string($responsetogetapikey);
$apikey=$pardotresponsetogetapikey->api_key;
$email='PROSPECTEMAIL';
$pardotresponse = callPardotApi('https://pi.pardot.com/api/prospect/version/4/do/create/email/'.$email, array(
'first_name' => 'PROSPECTNAME',
'last_name' => 'PROSPECTLASTNAME',
'state' => 'PROSPECTSTATE',
'phone' => 'PROSPECTPHONE',
'user_key' => 'YOURPARDOTKEY',
'api_key' => $apikey,
'YOURCUSTOMFIELDS' => $_SESSION['Pardot']['YOURCUSTOMFIELD'],
));
I am sure you imagined, but worth mentioning… if you wish to send custom data to your Pardot Instance, you will need to create the custom fields in Pardot first.
If you wish to review the response and parse to make sure the data sync is complete just var_dump the response and see what is made available to you.
echo "<pre>";
var_dump(simplexml_load_string($pardotresponse));
echo "</pre>";
Leave comment