VTiger CRM WebServices API实例
实例是用php代码访问VTiger CRM的数据 实例1:
实例1:
同学们可以很容易的把代码迁移到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 实例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 参考: vtiger WebServices API官方网站 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |