VTiger CRM WebServices API Instance

Source: Internet
Author: User
Tags datetime json pear php code urlencode ticket vtiger zend

Example is to access Vtiger CRM data in PHP code

Example 1: Login code Retrieve all helpdesk trouble sheets Create contact contacts retrieve a single ticket based on the object ID, print out the update a trouble ticket

Example 1:

Retrieving all modules that the logged-on user can access module name retrieves all the parameters of the Helpdesk failure order module


Students can easily migrate the code to Java/c++ 's client program.


You need to use the Pear command to install the Http_client Library for PHP, download and install the ZendFramework-1.6.1 class library separately.

Command: Pear install http_client, installation location/usr/share/pear/http/client.php

Edit/etc/php.ini include_path= ".:/ Usr/share/pear:/backup/zendframework-1.6.1-minimal/library "


Example 1 code:

<?php require_once ' http/client.php ';

Require_once ' zend/json.php '; URL path to vtiger/webservice.php like http://vtiger_url/webservice.php $endpointUrl = "http://localhost/vtigercrm/
Webservice.php "; 
Username of the user logged in.

$userName = "admin";

$HTTPC = new Http_client ();
Getchallenge request must be a GET request.
$httpc->get ("$endpointUrl operation=getchallenge&username= $userName");

$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Check for whether the requested operation is successful or not.
    if ($jsonResponse [' Success ']==false)//handle the failure case.

Die (' Getchallenge failed: '. $jsonResponse [' ERROR '] [' errormsg ']);
Operation is successful get the token from the reponse.
$challengeToken = $jsonResponse [' result '] [' token '];

Echo ' Challengetoken is '. $challengeToken; /***************************************///access Key of the useradmin, found on my preferences page.	$userAccessKey = ' xxxxxx '; Web Services acess key for user admin//create MD5 string concatenating user accesskey from my preference page//and t 
He challenge token obtained from Get challenge result.
$generatedKey = MD5 ($challengeToken. $userAccessKey);
Login request must be POST request. $httpc->post ("$endpointUrl", Array (' operation ' = ' login ', ' username ' = ' $userName, ' accessKey ' = $ge
Neratedkey), true);
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.
Die (' Login failed: '. $jsonResponse [' ERROR '] [' errormsg ']);

Echo ' <br/>login success ';
Login successful extract sessionId and userId from Loginresult to it can used for further calls. 
$sessionId = $jsonResponse [' result '] [' sessionname ']; $usErid = $jsonResponse [' result '] [' userId ']; Echo ' <br/>user ID is '.
$userId;
/***************************************///query Tickets//query to select data from the server.
$query = "SELECT * from HelpDesk;";
UrlEncode to as it sent over HTTP.
$queryParam = UrlEncode ($query);
SessionId is obtained from login result.
$params = "Sessionname= $sessionId &operation=query&query= $queryParam";
Query must be GET Request.
$httpc->get ("$endpointUrl $params");
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.

Die (' Query failed: '. $jsonResponse [' errormsg ']);

Array of Vtigerobjects $retrievedObjects = $jsonResponse [' result ']; Echo ' <br/>helpdesk tickets found '.

Count ($retrievedObjects); /***************************************///e.g. CrEate Contact//fill in the details of the Contacts.userid are obtained from Loginresult.
$contactData = Array (' LastName ' = ' 1 Jiang Valiant ', ' assigned_user_id ' = ' $userId);
Encode the object in JSON format to communicate with the server.
$objectJson = Zend_json::encode ($contactData);
Name of the module for which the entry have to be created.

$moduleName = ' Contacts ';
SessionId is obtained from Loginresult. $params = Array ("sessionname" = $sessionId, "Operation" = "create",//"element" and "$objectJson", "ElementType
"= $moduleName);
Create must be POST Request.
$httpc->post ("$endpointUrl", $params, True);
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
if ($jsonResponse [' Success ']==false)////handle the failure case.
Die (' Create failed: '. $jsonResponse [' ERROR '] [' errormsg ']); //$Savedobject = $jsonResponse [' result '];

$id = $savedObject [' id ']; Echo ' created contact, ID is '.
$id; Note:admin created contact 12x220 are ID of vtiger_ws_entity for contact, and is crmid in table vtiger_crmentity/
///retrive ticket//sessionid is obtained from Loginresult.

Echo ' <br/>query ticket ID 17x219 '; 
Ticket id $id = ' 17x219 ';
Contact ID//$id = ' 12x220 ';
$params = "Sessionname= $sessionId &operation=retrieve&id= $id";
Retrieve must be GET Request.
$httpc->get ("$endpointUrl $params");
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.
$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.

Die (' Retrieve failed: '. $jsonResponse [' ERROR '] [' errormsg ']);

$retrievedObject = $jsonResponse [' result ']; Echo ' <br/> array count '. Count ($rEtrievedobject);
$keys = Array_keys ($retrievedObject);

Print_r ($keys); while ($key = key ($retrievedObject)) {echo ' <br/> '. $key. ': ' .
  $retrievedObject [$key];
Next ($retrievedObject);
}/***************************************///update ticket//retrievedobject from previous example (Example 1).
Open, in progress,wait for Response, Closed $retrievedObject [' ticketstatus '] = ' Wait for Response ';
$retrievedObject [' description '] = ' xxxopen ';
Encode the object in JSON format to communicate with the server.

$objectJson = Zend_json::encode ($retrievedObject);
SessionId is obtained from the login operation result.
$params = Array ("sessionname" + $sessionId, "operation" = "Update", "element" = = $objectJson);
Update must be a POST request.
$httpc->post ("$endpointUrl", $params, True);
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']); Operation was successfulGet the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.
    Die (' <br/>update failed: '. $jsonResponse [' ERROR '] [' errormsg ']);
Echo ' <br/>update failed ';
else echo ' <br/>update success ';
Update Result object.

$updatedObject = $jsonResponse [' result ']; while ($key = key ($updatedObject)) {echo ' <br/> '. $key. ': ' .
  $updatedObject [$key];
Next ($updatedObject); }?>


The results of the operation are as follows:

Challengetoken is 5179C94D8EBCC
Login Success
User ID is 19x1
HelpDesk Tickets found 7
Query Ticket ID 17x219
Array Count 19
Ticket_no:tt99
Assigned_user_id:19x5
PARENT_ID:
Ticketpriorities:low
PRODUCT_ID:
Ticketseverities:minor
Ticketstatus:wait for Response
Ticketcategories:big problem
Update_log:ticket created. Assigned to User Jiang Yang Yangjiang – Sunday 21st April 11:34:44 AM by jiyang--//----Sunday 21st April 11:35:18 am by jiyang--//----Sunday 21st April 11:45:13 am to jiyang--//----Sunday 21st April 11:45:24 AM by jiyang--// ----Wednesday 24th April 11:03:50 PM by admin--//--Status Changed to Progress\. --Friday 26th April 07:38:17 AM by admin--//--Status Changed to open\. --Friday 26th April 07:42:44 AM by admin--//--
hours:0
days:0
Createdtime:2013-04-21 11:34:44
Modifiedtime:2013-04-26 08:24:40
from_portal:0
Modifiedby:19x1
Ticket_title:xxxxopen
Description:xxxopen
Solution:
id:17x219


Example 2 code:

<?php require_once ' http/client.php ';

Require_once ' zend/json.php '; URL path to vtiger/webservice.php like http://vtiger_url/webservice.php $endpointUrl = "http://localhost/vtigercrm/
Webservice.php "; 
Username of the user logged in.

$userName = "admin";

$HTTPC = new Http_client ();
Getchallenge request must be a GET request.
$httpc->get ("$endpointUrl operation=getchallenge&username= $userName");

$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Check for whether the requested operation is successful or not.
    if ($jsonResponse [' Success ']==false)//handle the failure case.

Die (' Getchallenge failed: '. $jsonResponse [' ERROR '] [' errormsg ']);
Operation is successful get the token from the reponse.
$challengeToken = $jsonResponse [' result '] [' token '];

Echo ' Challengetoken is '. $challengeToken; /***************************************///access Key of the useradmin, found on my preferences page.	$userAccessKey = ' xxxx '; Web Services acess key for user admin//create MD5 string concatenating user accesskey from my preference page//and t 
He challenge token obtained from Get challenge result.
$generatedKey = MD5 ($challengeToken. $userAccessKey);
Login request must be POST request. $httpc->post ("$endpointUrl", Array (' operation ' = ' login ', ' username ' = ' $userName, ' accessKey ' = $ge
Neratedkey), true);
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.
Die (' Login failed: '. $jsonResponse [' ERROR '] [' errormsg ']);

Echo ' <br/>login success ';
Login successful extract sessionId and userId from Loginresult to it can used for further calls. 
$sessionId = $jsonResponse [' result '] [' sessionname ']; $userId = $jsonResponse [' result '] [' userId ']; Echo ' <br/>user ID is '.
$userId;
/***************************************///query Tickets//query to select data from the server.
$query = "SELECT * from HelpDesk;";
UrlEncode to as it sent over HTTP.
$queryParam = UrlEncode ($query);
SessionId is obtained from login result.
$params = "Sessionname= $sessionId &operation=query&query= $queryParam";
Query must be GET Request.
$httpc->get ("$endpointUrl $params");
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.

Die (' Query failed: '. $jsonResponse [' errormsg ']);

Array of Vtigerobjects $retrievedObjects = $jsonResponse [' result ']; Echo ' <br/>helpdesk tickets found '.

Count ($retrievedObjects); /***************************************///e.g. CreaTe contact//fill in the details of the Contacts.userid are obtained from Loginresult.
$contactData = Array (' LastName ' = ' 1 Jiang Valiant ', ' assigned_user_id ' = ' $userId);
Encode the object in JSON format to communicate with the server.
$objectJson = Zend_json::encode ($contactData);
Name of the module for which the entry have to be created.

$moduleName = ' Contacts ';
SessionId is obtained from Loginresult. $params = Array ("sessionname" = $sessionId, "Operation" = "create",//"element" and "$objectJson", "ElementType
"= $moduleName);
Create must be POST Request.
$httpc->post ("$endpointUrl", $params, True);
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
if ($jsonResponse [' Success ']==false)////handle the failure case.
Die (' Create failed: '. $jsonResponse [' ERROR '] [' errormsg ']); $saVedobject = $jsonResponse [' result '];

$id = $savedObject [' id ']; Echo ' created contact, ID is '.
$id; Note:admin created contact 12x220 are ID of vtiger_ws_entity for contact, and is crmid in table vtiger_crmentity/
///retrive ticket//sessionid is obtained from Loginresult.

Echo ' <br/>query ticket ID 17x219 '; 
Ticket id $id = ' 17x219 ';
Contact ID//$id = ' 12x220 ';
$params = "Sessionname= $sessionId &operation=retrieve&id= $id";
Retrieve must be GET Request.
$httpc->get ("$endpointUrl $params");
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.
$jsonResponse = Zend_json::d ecode ($response [' body ']);
Operation is successful get the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.

Die (' Retrieve failed: '. $jsonResponse [' ERROR '] [' errormsg ']);

$retrievedObject = $jsonResponse [' result ']; Echo ' <br/> array count '. Count ($retRievedobject);
$keys = Array_keys ($retrievedObject);

Print_r ($keys); while ($key = key ($retrievedObject)) {echo ' <br/> '. $key. ': ' .
  $retrievedObject [$key];
Next ($retrievedObject);
}/***************************************///update ticket//retrievedobject from previous example (Example 1).
Open, in progress,wait for Response, Closed $retrievedObject [' ticketstatus '] = ' Wait for Response ';
$retrievedObject [' description '] = ' xxxopen ';
Encode the object in JSON format to communicate with the server.

$objectJson = Zend_json::encode ($retrievedObject);
SessionId is obtained from the login operation result.
$params = Array ("sessionname" + $sessionId, "operation" = "Update", "element" = = $objectJson);
Update must be a POST request.
$httpc->post ("$endpointUrl", $params, True);
$response = $httpc->currentresponse ();
Decode the JSON encode response from the server.

$jsonResponse = Zend_json::d ecode ($response [' body ']); Operation was successful GEt the token from the reponse.
    if ($jsonResponse [' Success ']==false)//handle the failure case.
    Die (' <br/>update failed: '. $jsonResponse [' ERROR '] [' errormsg ']);
Echo ' <br/>update failed ';
else echo ' <br/>update success ';
Update Result object.

$updatedObject = $jsonResponse [' result ']; while ($key = key ($updatedObject)) {echo ' <br/> '. $key. ': ' .
  $updatedObject [$key];
Next ($updatedObject); }?>


Operation Result:

Challengetoken is 5179e121a6c19
Login Success
User ID is 19x5
Total Modules 30
Vendors
Faq
Quotes
PurchaseOrder
SalesOrder
Invoice
Pricebooks
Calendar
Leads
Accounts
Contacts
Potentials
Products
Documents
Emails
HelpDesk
Events
Services
Servicecontracts
Pbxmanager
Assets
Smsnotifier
Modcomments
Projectmilestone
Projecttask
Project
Groups
Currency
Documentfolders
Companydetails
Module HelpDesk Details:
Label Trouble Ticket
Name HelpDesk
Createable true
Updateable true
Deleteable true
Retrieveable true
Total Fields 18
Field label creation time
Mandatory:false
Type Name:datetime
Type defaultvalue:
Default
Nillable:false
Editable:false
Field Label Day
Mandatory:false
Type Name:integer
Type defaultvalue:
Default
Nillable:false
Editable:true
Field Label description
Mandatory:false
Type Name:text
Type defaultvalue:
Default
Nillable:false
Editable:true
field label from the portal site
Mandatory:false
Type Name:boolean
Type defaultvalue:
Default
Nillable:false
Editable:true
Field Label Hour
Mandatory:false
Type Name:integer
Type defaultvalue:
Default
Nillable:false
Editable:true
Field label last Modified by
Mandatory:false
Type Name:reference
Type defaultvalue:
Default
Nillable:false
Editable:true
Field Label Modification time
Mandatory:false
Type Name:datetime
Type defaultvalue:
Default
Nillable:false
Editable:false
field label related objects
Mandatory:false
Type Name:reference
Type defaultvalue:
Default
Nillable:false
Editable:true
Field Label priority
Mandatory:false
Type Name:picklist
Type DefaultValue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.