Looking php code for auto Sync Jobs to Infusionsoft by Keap, For this you need to create a some custom fields in Keap so you need to know both API for auto sync data from ServiceM8 to Infusionsoft keap, they there is some limitations like using hook we need to Confirm our application so that will take a time or sometime they will reject so not possible to do.
It’s very simple as run one cron job and check latest updated products and sync that products into Keap.
Looking me to do All things for you ? Contact me
You can follow below step and archive your need if you are a developer then.
Step 1:
First you need to setup keap API and for that you need to download PHP SDK from here.
Now you need to update composer and then you will see all codes, and then you need to get clientId and clientSecret and need to set proper redirect URL. you will get Key ID and Secret from here
Step 2:
In second step we need to get ServiceM8 API key and that we will going to pass into api call as a Header Authorise parameters.
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
'accept: application/json',
'authorization: Basic <KEY HERE>'
));
So both configuration is ready now we just need to ready data regular bases from servicem8 and then pass that data into Keap.
Now you have how we can sync without using hook as in some of software are not providing hooks so in this case we need to check using cron and last update date. here we are doing same thing as reading data from Servicem8 and then passing full job and customer data with category into the infusionsoft (keap).
Step 3
Here in step3 we are going to setup file structure and writing code. So for Keap PHP SDK sdk we need to install composer and after that we can see like this
On above screenshot you can see some extra files that we created. one our useful files is infuRefreshToken.php and keap.php.
infuRefreshToken.php file is use for refreshing token, here first we need to authorise so they give tokens and using that token we need to regenerate within hour so we need to schedule one cron for that token generation.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once __DIR__.'/vendor/autoload.php';
$filename =__DIR__."/vendor/token_persits.txt"; // this is our token file which is we are storing into file
$infusionsoft = new \Infusionsoft\Infusionsoft(array(
'clientId' => '<YOUR CLIENT ID>',
'clientSecret' => '<YOUR SECRET>',
'redirectUri' => '<YOUR REDIRECT URL>',
));
if ($token = file_get_contents($filename)) {
$infusionsoft->setToken(unserialize($token));
}
$infusionsoft->refreshAccessToken();
$handle = fopen($filename, 'w');
// Write $somecontent to our opened file.
if (fwrite($handle, serialize($infusionsoft->getToken())) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
?>
So above file is only generating a token and write into file so we can use that generated token for writing searvice8 data into there.
keap.php this is very important file and here we are writing our all code to make it working.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once __DIR__.'/vendor/autoload.php';
$filename =__DIR__."/vendor/token_persits.txt"; // Token file
$infusionsoft = new \Infusionsoft\Infusionsoft(array(
'clientId' => '<YOUR CLIENT ID>',
'clientSecret' => '<YOUR SECRET>',
'redirectUri' => '<YOUR REDIRECT URL>',
));
// By default, the SDK uses the Guzzle HTTP library for requests. To use CURL,
// you can change the HTTP client by using following line:
// $infusionsoft->setHttpClient(new \Infusionsoft\Http\CurlClient());
// If the serialized token is available in the session storage, we tell the SDK
// to use that token for subsequent requests.
if ($token = file_get_contents($filename)) {
$infusionsoft->setToken(unserialize($token));
}
// If we are returning from Infusionsoft we need to exchange the code for an
// access token for authorization
if (isset($_GET['code']) ) {
$infusionsoft->requestAccessToken($_GET['code']);
// Save the serialised token to the current session for subsequent requests
$handle = fopen($filename, 'w');
// Write $somecontent to our opened file.
if (fwrite($handle, serialize($infusionsoft->getToken())) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
function _exec($api,$postRequest=null){
// this method is use for calling servicem8 API
$host="https://api.servicem8.com";
$cURLConnection = curl_init($host.$api);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERPWD, "<EMAIL>:<PWD>"); //not recommended
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
'accept: application/json',
'authorization: <KEY HERE>'
));
$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);
return json_decode($apiResponse);
}
function sync_data($infusionsoft,$data){
$refresh=false;
start:
try {
$email = $data['email'];
try {
$cid = $infusionsoft->contacts()->with('custom_fields')->where('email', $email)->first();
$cid->given_name=$data['fname'];
$cid->family_name=$data['lname'];
$email1 = new \stdClass;
$email1->field = 'EMAIL1';
$email1->email = $email;
$cid->email_addresses=[$email1];
$phone1 = new \stdClass;
$phone1->field = 'PHONE1';
$phone1->number = $data['phone'];
$phone2 = new \stdClass;
$phone2->field = 'PHONE2';
$phone2->number = $data['mobile'];;
$cid->phone_numbers=[$phone2,$phone1];
// here we need to pass custom field id and data
$fields=[];
$fields[]=array("id"=>"1","content"=>$data["job_id"]);
$fields[]=array("id"=>"3","content"=>$data["job_status"]);
$fields[]=array("id"=>"9","content"=>$data["job_address"]);
$fields[]=array("id"=>"11","content"=>$data["date"]);
$fields[]=array("id"=>"13","content"=>$data["job_badges"]);
$cid->custom_fields=$fields;
unset($cid->tag_ids);
unset($cid->ScoreValue);
unset($cid->last_updated_utc_millis);
$infusionsoft->contacts('xml')->addToGroup($cid->id, 211);//Update Tag
$cid->save();
return $cid;
} catch (\Infusionsoft\InfusionsoftException $e) {
$fields=[];
$fields[]=array("id"=>"1","content"=>$data["job_id"]);
$fields[]=array("id"=>"3","content"=>$data["job_status"]);
$fields[]=array("id"=>"9","content"=>$data["job_address"]);
$fields[]=array("id"=>"11","content"=>$data["date"]);
$fields[]=array("id"=>"13","content"=>$data["job_badges"]);
$email1 = new \stdClass;
$email1->field = 'EMAIL1';
$email1->email = $email;
$phone1 = new \stdClass;
$phone1->field = 'PHONE1';
$phone1->number = $data['phone'];
$phone2 = new \stdClass;
$phone2->field = 'PHONE2';
$phone2->number = $data['mobile'];;
$contact = ['given_name' => $data['fname'], 'family_name' => $data['lname'], 'email_addresses' => [$email1],"phone_numbers"=>[$phone2,$phone1],"custom_fields"=>$fields];
$cid=$infusionsoft->contacts()->create($contact);
return $cid;
}
} catch (\Infusionsoft\TokenExpiredException $e) {
throw $e;
}
}
if ($token=$infusionsoft->getToken()) {
date_default_timezone_set("Australia/Brisbane");
$filename=__DIR__."/last_run.txt";
if (file_exists($filename) && $date = file_get_contents($filename)) {
if(date("Y-m-d H:i:s",intval($date))==null || $date=="")
$date=date("Y-m-d H:i:s", filemtime($filename));
}
else $date=date("Y-m-d H:i:s");
$data=[];
$jobs = _exec("/api_1.0/job.json?%24filter=edit_date%20gt%20'".urlencode($date)."'");
try{
if(count($jobs)>0){
foreach($jobs as $job){
$data['date']=$job->date;
$data['job_id']=$job->generated_job_id;
$data['job_status']=$job->status;
$data['job_status']=$job->status;
$data['job_address']=$job->job_address;
$data['job_badges']=(!empty($job->badges) && is_array($job->badges))?implode(",",$job->badges):"";
$contacts=_exec("//api_1.0/jobcontact.json?%24filter=job_uuid%20eq%20'".$job->uuid."'");
if(is_array($contacts) && count($contacts)>0){
$contact=end($contacts);
$data['fname']=$contact->first;
$data['lname']=$contact->last;
$data['email']=$contact->email;
$data['mobile']=$contact->mobile;
$data['phone']=$contact->phone;
}
if(!empty($data['email'])){
$res=sync_data($infusionsoft,$data);
error_log("\n".date("d-m-Y H:i:s").":::\n".print_r($res,true)."\n", 3, __DIR__."/logs.log");
}
}
}
else{
error_log("\n".date("d-m-Y H:i:s").":::\n NO JOB update found\n", 3, __DIR__."/logs.log");
}
$handle = fopen($filename, 'w');
// Write $somecontent to our opened file.
if (fwrite($handle, date("Y-m-d H:i:s")) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
catch(Exception $e){
echo "<pre>";
print_r($e);
exit;
}
} else {
echo '<a href="' . $infusionsoft->getAuthorizationUrl() . '">Click here to authorize</a>';
}
So above code is used for sync and you will understand that and looking help just contact me, i will give my services so making working your synchronisation with any third party solutions.
Step 4:
Now once done we need to setup two cron jobs to make it working as below
Looking more help for your integration like ZOHO, WordPress, Mobile app, Contact me
Buy this working code in 25 USD so save your time to do RND
Contact me to buy this code.
Thank you
Leave a Reply