加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

VTiger CRM WebServices API实例

发布时间:2020-12-17 00:21:51 所属栏目:安全 来源:网络整理
导读:实例是用php代码访问VTiger CRM的数据 实例1: 登录代码 检索所有HelpDesk 故障单 创建Contact联系人 根据object id检索某一个故障单,打印出来 Update更新一个故障单 实例1: 检索登录用户可以访问的所有模块module名称 检索HelpDesk故障单模块的所有参数

实例是用php代码访问VTiger CRM的数据

实例1:

  1. 登录代码
  2. 检索所有HelpDesk 故障单
  3. 创建Contact联系人
  4. 根据object id检索某一个故障单,打印出来
  5. Update更新一个故障单

实例1:

  1. 检索登录用户可以访问的所有模块module名称
  2. 检索HelpDesk故障单模块的所有参数


同学们可以很容易的把代码迁移到Java/C++的客户端程序上去。


需要用pear命令给php安装HTTP_Client库,单独下载安装ZendFramework-1.6.1类库。

命令:pear install HTTP_Client,安装位置 /usr/share/pear/HTTP/Client.php

编辑 /etc/php.ini?include_path=".:/usr/share/pear:/backup/ZendFramework-1.6.1-minimal/library"


实例1 代码:

<?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 who is to 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::decode($response['body']);

//check for whether the requested operation was successful or not.
if($jsonResponse['success']==false) 
    //handle the failure case.
    die('getchallenge failed:'.$jsonResponse['error']['errorMsg']);

//operation was successful get the token from the reponse.
$challengeToken = $jsonResponse['result']['token'];
echo 'challengeToken is ' .$challengeToken;

/***************************************/
//access key of the user admin,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 the 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'=>$generatedKey),true);
$response = $httpc->currentResponse();
//decode the json encode response from the server.
$jsonResponse = Zend_JSON::decode($response['body']);

//operation was 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 its 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::decode($response['body']);

//operation was 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 is 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 has to be created.
// $moduleName = 'Contacts';

//sessionId is obtained from loginResult.
// $params = array("sessionName"=>$sessionId,"operation"=>'create',//    "element"=>$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::decode($response['body']);

//operation was 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   12 is id of vtiger_ws_entity for Contact,220 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::decode($response['body']);
//operation was 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",true);
$response = $httpc->currentResponse();
//decode the json encode response from the server.
$jsonResponse = Zend_JSON::decode($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);
}
?>


运行结果如下:

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 杨江 -- Sunday 21st April 2013 11:34:44 AM by jiyang--//-- -- Sunday 21st April 2013 11:35:18 AM by jiyang--//-- -- Sunday 21st April 2013 11:45:13 AM by jiyang--//-- -- Sunday 21st April 2013 11:45:24 AM by jiyang--//-- -- Wednesday 24th April 2013 11:03:50 PM by admin--//-- Status Changed to In Progress. -- Friday 26th April 2013 07:38:17 AM by admin--//-- Status Changed to Open. -- Friday 26th April 2013 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


实例2代码:

<?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 who is to 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::decode($response['body']);

//check for whether the requested operation was successful or not.
if($jsonResponse['success']==false) 
    //handle the failure case.
    die('getchallenge failed:'.$jsonResponse['error']['errorMsg']);

//operation was successful get the token from the reponse.
$challengeToken = $jsonResponse['result']['token'];
echo 'challengeToken is ' .$challengeToken;

/***************************************/
//access key of the user admin,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 the challenge token obtained from get challenge result. 
$generatedKey = md5($challengeToken.$userAccessKey);
//login request must be POST request.
$httpc->post("$endpointUrl",true);
$response = $httpc->currentResponse();
//decode the json encode response from the server.
$jsonResponse = Zend_JSON::decode($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);
}
?>


运行结果:

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 故障单
name HelpDesk
createable true
updateable true
deleteable true
retrieveable true
total fields 18
?? field label 创建时间
?? ?? ?? mandatory: false
?? ?? ?? type name: datetime
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: false
?? field label 天
?? ?? ?? mandatory: false
?? ?? ?? type name: integer
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 描述
?? ?? ?? mandatory: false
?? ?? ?? type name: text
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 来自门户网站
?? ?? ?? mandatory: false
?? ?? ?? type name: boolean
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 小时
?? ?? ?? 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 修改时间
?? ?? ?? mandatory: false
?? ?? ?? type name: datetime
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: false
?? field label 相关对象
?? ?? ?? mandatory: false
?? ?? ?? type name: reference
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 优先级
?? ?? ?? mandatory: false
?? ?? ?? type name: picklist
?? ?? ?? type defaultValue: Low
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 产品名称
?? ?? ?? mandatory: false
?? ?? ?? type name: reference
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 严重程度
?? ?? ?? mandatory: false
?? ?? ?? type name: picklist
?? ?? ?? type defaultValue: Minor
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 负责人
?? ?? ?? mandatory: true
?? ?? ?? type name: owner
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 解决方案
?? ?? ?? mandatory: false
?? ?? ?? type name: text
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 状态
?? ?? ?? mandatory: true
?? ?? ?? type name: picklist
?? ?? ?? type defaultValue: Open
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 故障单编号
?? ?? ?? mandatory: false
?? ?? ?? type name: string
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: false
?? field label 标题
?? ?? ?? mandatory: true
?? ?? ?? type name: string
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label 更新历史
?? ?? ?? mandatory: false
?? ?? ?? type name: text
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: true
?? field label ticketid
?? ?? ?? mandatory: false
?? ?? ?? type name: autogenerated
?? ?? ?? type defaultValue:?
?? ?? ?? default:?
?? ?? ?? nillable: false
?? ?? ?? editable: false
Contacts found 2
object Array
object attributes 44
object attribute

参考:

vtiger WebServices API官方网站

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读