How can I achieve goal through cURL?


(Steve O'Rourke) #1

I am adding contact to Infusionsoft from our web page through cURL like this url (https://api.infusionsoft.com/crm/rest/v1/contacts?access_token=) and post array data with json_encode($data) format. Bellow is my code :
$sPostData = json_encode($dataArr);
$uRL = ‘https://api.infusionsoft.com/crm/rest/v1/contacts?access_token=’;
$headers = array(‘Content-Type: application/json’);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uRL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPostData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );

But, after adding a contact from our website to Infusionsoft how can I achieve goal through cURL? what will be the cURL url like(https://api.infusionsoft.com/crm/rest/v1/contacts?access_token=) and what will be the post data array, and how can I pass this data ($options[‘integration’] = ‘su102’, $options[‘callName’] = ‘timesharesurveycouk’ and contact_id) in an array? Any code?
Can anybody help me?


(Nicholas Trecina) #2

Hi @Steve_O_Rourke, I would recommend using the Infusionsoft PHP SDK. It will facilitate both creating contacts and achieving goals so you don’t have to manually build the requests yourself.

Creating a contact should look something like this:

$infusionsoft->contacts()->create([
	'given_name' => 'Testing',
	'email_addresses' => [[
		'field': 'EMAIL1',
		'email': 'test@example.com'
	]]
]);

Achieving a goal should look like this:

$infusionsoft->funnels()->achieveGoal('integration', 'callName', $contactId);

(Steve O'Rourke) #3

$infusionsoft->funnels()->achieveGoal(‘integration’, ‘callName’, $contactId);
But I am using cURL post, so how can I post this through cURL? And what will be the cURL post url?


(Michael Fairchild) #4

@Steve_O_Rourke Why do you want to use cURL? The SDK does use cURL, just makes it easier to access each method.


(Steve O'Rourke) #5

I have already stored full token info in the database, if I use this ( $infusionsoft->funnels()->achieveGoal(‘integration’, ‘callName’, $contactId)) code, then how can I pass full token or only accessToken with code, I want to pick up token/accessToken from the database and need to pass it through code, but how, please can you send me the full code?

Below given full token(in json_encode format):
$token = ‘O:18:“InfusionsoftToken”:4:{s:11:“accessToken”;s:24:“xxxxxxxxxxxxxxx”;s:12:“refreshToken”;s:24:“xxxxxxxxxxxxxxxxx”;s:9:“endOfLife”;i:1538906402;s:9:“extraInfo”;a:2:{s:10:“token_type”;s:6:“bearer”;s:5:“scope”;s:27:“full|su102.infusionsoft.com”;}}’;


(Tom Scott) #6

Per the XML-RPC developer docs:
All requests except authentication requests made to the Infusionsoft XML-RPC API will be an HTTP POST to https://api.infusionsoft.com/crm/xmlrpc/v1?access_token=123abc, and all requests made after authenticating with OAuth 2.0 must have an access token appended as the access_token query parameter in order to authenticate the request.

If using the Infusionsoft PHP SDK, you will need to store the access token in the session as $_SESSION['token'], as shown on the SDK readme.

Additionally, Access and Refresh tokens are secret information, and are as good as a password to your account. I would not recommend ever posting them to a public forum. I have removed them from your previous post.


(Steve O'Rourke) #7

FOR ACHIEVE GOAL ::
This is my code below :
require_once ‘vendor/autoload.php’;

$infusionsoft = new \Infusionsoft\Infusionsoft(array(
‘clientId’ => ‘****’,
‘clientSecret’ => ‘****’,
‘redirectUri’ => ‘https://mr-panel.com/settoken.php
));

$res = $infusionsoft->funnels()->achieveGoal(‘su102’, ‘TimeshareSurvey’, ‘193215’);

But I got an error below :
Fatal error : Uncaught exception ‘Infusionsoft\TokenExpiredException’ in /home/mrpanel/public_html/vendor/infusionsoft/php-sdk/src/Infusionsoft/Infusionsoft.php:437 Stack trace: #0 /home/mrpanel/public_html/vendor/infusionsoft/php-sdk/src/Infusionsoft/Api/FunnelService.php(15): Infusionsoft\Infusionsoft->request(‘FunnelService.a…’, ‘su102’, ‘TimeshareSurvey’, ‘193215’) #1 /home/mrpanel/public_html/test30.php(48): Infusionsoft\Api\FunnelService->achieveGoal(‘su102’, ‘TimeshareSurvey’, ‘193215’) #2 {main} thrown in /home/mrpanel/public_html/vendor/infusionsoft/php-sdk/src/Infusionsoft/Infusionsoft.php on line 437

What is wrong with me? And one more thing that I have accessToken already stored in database, if need to pass accessToken from DB then how it will be pass through code? I don’t need to authenticate and get the accessToken, before I told you that, already I have accessToken and it has been stored in DB. Can you send me the full code?


(Tom Scott) #8

TokenExpiredException implies that your AccessToken is expired; did you use the RefreshToken to generate a new one to use within 24 hours of the request?