Help understanding GET curl calls?

I’d like to experiment with connecting a Wordpress website to a SuiteCRM via API.

In short, what I want to do is, once a new user registers, fire a Curl call to the CRM to check if the user exists (either as verified by email address or creating a field in the CRM for their Wordpress username), and if the user doesn’t exist, then create a new one in the CRM.

I know the very basics of how to make a Curl call in PHP, but what I don’t understand how to loop through it, or even really how that data is brought into PHP.

From the SuiteCRM documentation, the url for filtering a record would be

{{suitecrm.url}}/Api/V8/module/Accounts?fields[Accounts]=name,account_type&filter[operator]=and&filter[account_type][eq]=Customer

That would check for an “Account” with the type = Customer.

So mine would probably be something like

{{suitecrm.url}}/Api/V8/module/Targets?fields[Targets]=name,target_email&filter[operator]=and&filter[target_email][eq]=<their-email>

So, once that Curl gets fired on the Wordpress server, how do I parse that data, and then do the rest of the function with it? Do I somehow just set a variable to be the JSON that the SuiteCRM returns?

Yeah so when you call those URLs through curl, you are going to get the JSON back. Look at the curl_exec() PHP function which is used to get the content returned (or false if there is a failure). You can store that content into a variable and use the PHP function json_decode($variable) on it to then have it build an object with it. From there it is just a matter of using the object to get access to its properties etc.

// Setup curl
$ch = curl_init();

// Set some options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// Send the request and store the returned JSON into $content
$content = curl_exec($ch);

// Make sure to check it isn't false
if ($content !== false) {
   $obj = json_decode($content);
   // Work with $obj or better yet put it all in a function and return this object to the rest of your code
} else {
   // Oooh something went wrong, maybe log it
}

This object you get back after your json_decode is going to have properties and arrays that you can drill down into and loop over. It can be treated as a normal object.

Hope that makes sense. :slight_smile:

I think the makes sense… So the GET from the CRM will return a JSON object (I’m not sure what happens if there are no users that match, but that’s more a SuiteCRM question), and I can make a normal array out of that.

Then, I guess I would fire another Curl call to the CRM, if there’s no match, to create the new user

So it’s something like:
User registers on Wordpress site
Curl call to CRM to check if there’s a Lead/Target that has that email address
If there’s none: Curl call back to CRM to make a new Lead/Target - or - if there is already one with that email address: update the record to indicate that they registered on the site

I guess one possible obstacle is that the CRM has “Leads” and “Targets.” Targets being basically anybody with a phone number or email address, and Leads being somebody that has expressed interest. If a sales rep calls / contacts a person when they are a “Target” and they express interest, they can be converted to a “Lead.”

I think I’ll have to check for BOTH Leads and Targets when the Wordpress user registers. I’m not sure if I would do that in a single GET or if I would have to do a single GET for the Lead, and another for the Target, if that makes sense.

Yes, that is typically how it works. At least in most of the CRMs I have worked with that is how it is typically done. You query to check if they exist, if not, you make another call to create.

:slight_smile:

Any tips for how to verify if the User/Person exists on both sides?

Email seems like the most logical way to do it, although some people do “share” an email address (husband/wife/whatever).

If you mean for identifying a particular user and person from one another I try NOT to use email. I mean you can treat it as unique but the problem you are going to have later is that people may want to change their email. It is more common than you think. Usually I try to setup a system where I use a unique ID I assign to the user and allow the system to find a user by that ID and by their email. That way you can treat the email as another field and not the primary key.

One crm I work with typically uses the email field and it has caused us problems through the years as people change jobs and want to take their accounts with them but obviously can’t take their email address with them as well.

Anything on the client side should probably be something you pulled from the server side at some point, so you should be able to have their IDs on both sides.

:slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.