Closed jakeperrin closed 6 years ago
Hey @jakeperrin, when is it that you'd like the Contact ID returned?
Anytime the user is on the site. Is it stored in a Cookie or some other easy way to access the Contact ID?
Take a look at the contact_view
api call - https://www.activecampaign.com/api/example.php?call=contact_view. You'll need some identifying information on the user to use that endpoint but it's your best bet for what you're attempting to do.
I updated the URL, I updated the API Key and I get this error...
"Result: FAILED"
The identifying information is what I am having a hard time with. I don't know what my options are. Is there a cookie with the contact hash in it or something else?
Could you post a snippet of the code that you're using? It's hard to be able to tell you what's going wrong without being able to see the code itself.
We do use cookies for our platform but they're unrelated to this API and are not publicly accessible.
@jakeperrin Please wrap the code in triple back ticks as shown here so this is readable: https://help.github.com/articles/creating-and-highlighting-code-blocks/
Thank you! :)
<?php
$url = 'http://getkion.api-us1.com';
$params = array(
'api_key' => 'hidden',
'api_action' => 'contact_view',
'api_output' => 'json',
'id' => 101577,
);
$query = "";
foreach( $params as $key => $value ) $query .= urlencode($key) . '=' . urlencode($value) . '&';
$query = rtrim($query, '& ');
$url = rtrim($url, '/ ');
if ( !function_exists('curl_init') ) die('CURL not supported. (introduced in PHP 4.0.2)');
if ( $params['api_output'] == 'json' && !function_exists('json_decode') ) {
die('JSON not supported. (introduced in PHP 5.2.0)');
}
$api = $url . '/admin/api.php?' . $query;
$request = curl_init($api); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
$response = (string)curl_exec($request); // execute curl fetch and store results in $response
curl_close($request); // close curl object
if ( !$response ) {
die('Nothing was returned. Do you have a connection to Email Marketing server?');
}
$result = unserialize($response);
echo 'Result: ' . ( $result['result_code'] ? 'SUCCESS' : 'FAILED' ) . '<br />';
echo 'Message: ' . $result['result_message'] . '<br />';
echo 'The entire result printed out:<br />';
echo '<pre>';
print_r($result);
echo '</pre>';
echo 'Raw response printed out:<br />';
echo '<pre>';
print_r($response);
echo '</pre>';
echo 'API URL that returned the result:<br />';
echo $api;
?>
@jakeperrin I see the issue - you're calling the function unserialize
to parse the response, but you've requested that JSON be returned. Change $result = unserialize($response);
to $result = json_decode($response, true);
and it should work just fine.
That worked! But now how do I get that identifying piece to pass to the script?
Two ways. One is to slightly rewrite the above script, and the second is to use the API wrapper offered in this repo. Here's how I'd suggest rewriting that above script, though I'd suggest using the wrapper in this repo as it's less code for you to write. Here first is the above script rewritten:
<?php
$url = 'https://getkion.api-us1.com';
$params = array(
'api_key' => 'hidden', // UPDATE ME
'api_action' => 'contact_view',
'api_output' => 'json',
'id' => 101577,
);
$query = "";
foreach( $params as $key => $value ) $query .= urlencode($key) . '=' . urlencode($value) . '&';
$query = rtrim($query, '& ');
$url = rtrim($url, '/ ');
$api = $url . '/admin/api.php?' . $query;
$request = curl_init($api); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
$response = (string)curl_exec($request); // execute curl fetch and store results in $response
curl_close($request); // close curl object
if ( !$response ) {
die('Nothing was returned. Do you have a connection to Email Marketing server?'); // replace this with something useful
}
$result = json_decode($response);
$response->id; // That's the id of the contact you're looking for, either echo this into your HTML or pass it to another function
And here's how you'd do this with the wrapper:
$ac = new ActiveCampaign("API_URL", "API_KEY");
$contact = $ac->api('contact/view?id=101577');
$contact->id;
But... The id in the params. I don't know it...
$params = array(
'api_key' => 'hidden', // UPDATE ME
'api_action' => 'contact_view',
'api_output' => 'json',
'id' => 101577,
);
Also, the id in the wrapper, I don't know it...
$contact = $ac->api('contact/view?id=101577');
So how do I get the ID?
Hey @jakeperrin, please refer to the documentation here that shows you can use the email address of the contact to get their ID. This would be:
$params = array(
'api_key' => 'hidden', // UPDATE ME
'api_action' => 'contact_view',
'api_output' => 'json',
'email' => 'example@example.org,
);
//
$contact = $ac->api('contact/view?email=example@example.org');
Got it. However...
I don't know their email. I was hoping to get that from ActiveCampaign. They have opted in with a form or they came from a link in a newsletter. You guys are tracking them some how with site tracking. We can see every page the user visits in ActiveCampaign...
So how do we get that identifying piece from you?
@jakeperrin it sounds like you need to talk to our support team, they can help you figure out a way to do this, but the GitHub issues tracker is the wrong place for this discussion. Please visit our contact page and use live chat or a call to talk to someone about the best way to go about doing this.
Thank you @bartboy011 !
@jakeperrin It looks like this is resolved, could you mark this issue as closed?
Do you guys have a way, maybe with the API, to send the Contact ID back to the site? I would like to grab that Contact ID with Google Tag Manager and use it else where.