Skip to content

Commit cd680ff

Browse files
authored
update version, composer, requires now redis, add .env and better… (#42)
update version, composer, requires now redis, add .env and better tes…
2 parents 2bd9cdd + 63220b2 commit cd680ff

File tree

13 files changed

+751
-566
lines changed

13 files changed

+751
-566
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ZOHO_AUTH_REFRESH_TOKEN=
2+
ZOHO_CLIENT_ID=
3+
ZOHO_CLIENT_SECRET=

README.md

Lines changed: 19 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
Zoho CRM library for PHP 5.5+
1+
Zoho CRM library for PHP
22
=============================
33

4-
The Zoho CRM library is a specialized XML wrapper for make request to zoho API, base on another vendor from [vaish](https://github.com/vaish/zohocrm-php), but this have some overpower :p.
4+
The Zoho CRM library is a simple wrapper around Zoho API
55

6-
I often found myself repeating the same pattern for XML manipulation with this API over and over. This library implements that pattern.
7-
8-
At it's heart, the library maps XML string to entity classes elements for interact like PHP value objects.
9-
10-
The following assumptions are made:
11-
12-
* XML namespaces are used everywhere using psr-0 for autoload class, same with composer.
13-
* All XML elements map to entities PHP classes.
14-
* Elements are represented by classes entities. A class(entity) extends of `Zoho\CRM\Wrapper\Element`, this gives you the ability to access the inherited method "deserializeXml()" for conver the values of xml into the object.
15-
16-
This is not your average XML library. The intention is not to make this super
17-
simple, but rather very powerful for complex XML applications.
6+
At MC we work a lot with Zoho and we found ourself repeting the same patter over and over that's why we build this library.
187

198
Installation
209
------------
@@ -28,7 +17,6 @@ Let's start
2817
The first thing that you have to do is use the namespaces for access to all the classes of the package, then, there are two fases for begin with the interaction:
2918

3019
1. Wrap values from array to entity, this have restrictions for the names of the fields, continue reading
31-
2. If you already have your entity with values, just jump to [Mapping XML to entities elements](#mapping-xml-to-entities-elements)
3220

3321
```php
3422
<?php
@@ -47,134 +35,50 @@ $request = array(
4735
'phone' => '809789654'
4836
);
4937

50-
// From array values to xml valid entity string
38+
// From array we need to clean its keys
5139
$lead = new Lead();
52-
$xmlstr = $lead->serializeXml($request);
53-
```
54-
At this part you have an **xvseL** (xml valid string entity Lead), this is nothing more than and entity Lead prepared for be parse to object, something like this:
55-
56-
```xml
57-
<Lead>
58-
<First_Name>Test_fname</First_Name>
59-
<Last_Name>Test_lname</Last_Name>
60-
<Phone>809789654</Phone>
61-
<Email>[email protected]</Email>
62-
<Programs>Unspecified</Programs>
63-
</Lead>
64-
```
40+
$data = $lead->cleanParams($request);
6541

6642
The above values of the **$request** array can be taken from POST if you are using forms in landing page :D, just be sure that all the keys(name of the field on html) in the array have to be valid properties in the entity Lead of zoho, default properties can be found on documentation [here](https://www.zoho.com/crm/help/api/modules-fields.html#Leads) or make sure that those properties exist in your account if you custom your Lead on Zoho; the following convention are made:
6743

6844
- Name of fields are not CamelCase.
6945
- Name with space between words, space is substituted by an "_".
7046
- Need to clean(unset) from the array all the values that are not part of the entity, if you dont wanna make this, create another clean array.
7147

72-
73-
Mapping XML to entities elements
74-
--------------------------------
75-
76-
Normally when writing an entity class parser using this tool, there will be a number of elements that make sense to create using classes for.
77-
78-
A great example would be the `Lead` entity, is part by default of the package, can be found inside `Zoho\CRM\Entities\Lead` element:
79-
80-
```php
81-
class Lead
82-
{
83-
/**
84-
* Zoho CRM user to whom the Lead is assigned.
85-
*
86-
* @var string
87-
*/
88-
private $Lead_Owner;
89-
90-
/**
91-
* Salutation for the lead
92-
*
93-
* @var string
94-
*/
95-
private $Salutation;
96-
97-
/**
98-
* First name of the lead
99-
*
100-
* @var string
101-
*/
102-
private $First_Name;
103-
104-
/**
105-
* The job position of the lead
106-
*
107-
* @var string
108-
*/
109-
private $Title;
110-
111-
/* etc, others fields... */
112-
}
113-
```
114-
115-
You can use this entity like a been, and make your own implementation of XML assings, but if you wanna use this for mapping xml, it recommended extends the entity of `Zoho\CRM\Wrapper\Element`, something like this:
116-
117-
```php
118-
use Zoho\CRM\Wrapper\Element;
119-
120-
class Lead extends Element
121-
{
122-
/* ...fields... */
123-
}
124-
```
125-
126-
Now for load the **xvseL** into the object Lead just call the method of the parent `deserializeXml(string $xvsel)`:
127-
128-
```php
129-
use Zoho\CRM\Entities\Lead;
130-
131-
$xvsel = '
132-
<Lead>
133-
<First_Name>Test_fname</First_Name>
134-
<Last_Name>Test_lname</Last_Name>
135-
<Phone>809789654</Phone>
136-
<Email>[email protected]</Email>
137-
<Programs>Unspecified</Programs>
138-
</Lead>';
139-
140-
$lead = new Lead();
141-
if($lead->deserializeXml($xvsel))
142-
{
143-
// Nice, now you have the entity with the values loaded from a string, F**k yeah..!
144-
/* Remember that you can set more parameters to the entity
145-
$lead->Lead_Owner = 'Test Owner Martinez';
146-
$lead->Lead_Source = 'http://someweirdsite.xxx';
147-
$lead->Member = '0001'; // This can be setted too :D
148-
*/
149-
echo 'Success, continue using your entity, the xvsel was parsed great...!';
150-
}else
151-
echo 'The xml could not be parsed, please check the syntax';
152-
}
15348
```
15449
Now the next part is interact with zoho api using the client, first thing, create a `ZohoClient` with your authtoken valid: **Set the module**, for now just Leads, on future or contributing the [missing modules](https://www.zoho.com/crm/help/api/modules-fields.html)
15550

15651
```php
15752
use Zoho\CRM\ZohoClient;
15853

159-
$ZohoClient = new ZohoClient('YOUR_TOKEN'); // Make the connection to zoho api
54+
55+
$ZohoClient = new ZohoClient(); // Make the connection to zoho api
56+
$ZohoClient->setAuthRefreshToken(getenv('ZOHO_AUTH_REFRESH_TOKEN'));
57+
$ZohoClient->setZohoClientId(getenv('ZOHO_CLIENT_ID'));
58+
$ZohoClient->setZohoClientSecret(getenv('ZOHO_CLIENT_SECRET'));
59+
$refresh = $ZohoClient->generateAccessTokenByRefreshToken();
60+
16061
$ZohoClient->setModule('Leads'); // Set the module
16162

162-
$validXML = $ZohoClient->mapEntity($lead); // Entity lead created with $xvsel
16363
```
16464

16565
Users of the .eu domain (i.e. your CRM URL is crm.zoho.eu, rather than crm.zoho.com) should call `setEuDomain()` after instantiating ZohoClient, i.e.
16666

16767
```php
168-
$ZohoClient = new ZohoClient('YOUR_TOKEN'); // Make the connection to zoho api
68+
$ZohoClient = new ZohoClient(); // Make the connection to zoho api
69+
$ZohoClient->setAuthRefreshToken(getenv('ZOHO_AUTH_REFRESH_TOKEN'));
70+
$ZohoClient->setZohoClientId(getenv('ZOHO_CLIENT_ID'));
71+
$ZohoClient->setZohoClientSecret(getenv('ZOHO_CLIENT_SECRET'));
72+
$refresh = $ZohoClient->generateAccessTokenByRefreshToken();
16973
$ZohoClient->setEuDomain();
17074
$ZohoClient->setModule('Leads'); // Set the module
17175
```
17276

173-
The last part if make the call, `$validXML` content returned by `mapEntity(Lead $lead)` is gonna be the xml that will be send to zoho, this is the final string created before the call to ws
77+
The last part if make the call, `$data` content returned by `cleanParams(Lead $lead)` is gonna be the array that will be send to zoho, this is the final json string created before the call to ws
17478

17579
Now make the call to ws and get the response object:
17680
```php
177-
$response = $ZohoClient->insertRecords($validXML);
81+
$response = $ZohoClient->insertRecords($data);
17882
```
17983

18084
The object Response returned in located in `Zoho\CRM\Request`, contain the code, message, method, module, records, record id, uri and xml returned by zoho, this can be accessed by getters.

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
],
2727
"require": {
2828
"php": ">=5.5",
29-
"guzzlehttp/guzzle": "^6.3"
29+
"guzzlehttp/guzzle": "^6.3",
30+
"ext-redis": ">=2.2.7"
3031
},
3132
"autoload": {
3233
"psr-4": {
@@ -35,7 +36,9 @@
3536
},
3637
"require-dev": {
3738
"codeception/verify": "^0.1",
38-
"friendsofphp/php-cs-fixer": "^2.2"
39+
"codeception/codeception": "^2.4",
40+
"friendsofphp/php-cs-fixer": "^2.2",
41+
"vlucas/phpdotenv": "^4.1"
3942
},
4043
"extra": {
4144
"branch-alias": {

index.php

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)