使用Web服务将引号导入vtiger crm

时间:2013-01-25 14:32:40

标签: php web-services import vtiger

我需要将引号导入vtiger。 我发现可以使用vtiger Web服务API

完成

我找到了参考手册: https://wiki.vtiger.com/archives/index.php/vtiger510:Webservice_reference_manual

但我找不到任何示例PHP脚本,也不能找到我需要传递给webservice.php的数据字段。

请帮助,我需要一些指导。

2 个答案:

答案 0 :(得分:3)

我做过类似的事情,我有一个快速而且相当脏的工作解决方案:

<?php

function createOffer($account_id,$subject,$offerlanguage, $totalamount,$date_submission,$date_decision,$date_start,$assigned_user_id,$quotestage,$winningchance,$description,$productarray){

        global $adb;

        $endpointUrl = "[your URL]/webservice.php";
        $userName="admin";
        $userAccessKey = '[your accesskey]';

        $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'];
        //create md5 string concatenating user accesskey from my preference page
        //and the challenge token obtained from get challenge result.
        $generatedKey = md5($challengeToken.$userAccessKey);

        //getchallenge request must be a GET 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']);

        //login successful extract sessionId and userId from LoginResult to it can used for further calls.
        $sessionId = $jsonResponse['result']['sessionName'];
        $userId = $jsonResponse['result']['userId'];

        $currency_id=1;
        $params =  array('description'=>$description,'subject'=>$subject,'quotestage'=>$quotestage,'assigned_user_id'=>'2x'.$assigned_user_id,'account_id'=>'3x'.$account_id,'cf_682'=>$offerlanguage,'currency_id'=>'21x'.$currency_id,'taxtype'=>'group','cf_683'=>$date_submission,'cf_684'=>$date_decision,'cf_685'=>$date_start,'cf_766'=>$winningchance);

        $urlArgs = "?&total=".$totalamount;
        //encode the object in JSON format to communicate with the server.
        $objectJson = Zend_JSON::encode($params);
        //name of the module for which the entry has to be created.
        $moduleName = 'Quotes';
        //sessionId is obtained from loginResult.
        $params = array("sessionName"=>$sessionId, "operation"=>'create', "element"=>$objectJson, "elementType"=>$moduleName);
        //Create must be POST Request.
        $httpc->post($endpointUrl.$urlArgs, $params, true);
        $response = $httpc->currentResponse();
        //decode the json encode response from the server.
        $jsonResponse = Zend_JSON::decode($response['body']);

        $savedObject = $jsonResponse['result'];
        $id = $savedObject['id'];   

        $id=str_replace("13x", "", $id);

        echo $id." offer: ".$subject." created for amount ".$totalamount." for customer: ".$account_id." assigned to: ".$assigned_user_id;

        return $id;

    }

如您所见,还有一些自定义字段,您可以看到我是如何处理这些字段的。

您可以像这样调用此函数:

createOffer($account_id, $subject, $offerlanguage, $totalamount, $date_submission, $date_decision, $date_start, $assigned_user_id, $quotestage, $winningchance, $description, $productarray)

然后你也需要添加产品,我发现这是最容易通过一个单独的功能,因为每个报价可以有更多的产品......

<?php

function createProducts($productarray,$id) {
    $counter = 1;
    foreach ($productarray as $prod) {
        $query ="insert into vtiger_inventoryproductrel(id, productid, sequence_no, quantity, listprice) values(?,?,?,?,?)";
        $qparams = array($id,$prod['prod'],$counter,$prod['pcs'],$prod['price']);
        $productadded=$adb->pquery($query,$qparams);
        $counter=$counter+1;
    }
}

像这样使用它:

$prodlist = array();

array_push($prodlist,array('prod'=>"prod1",'pcs'=>2,'price'=>1000));
array_push($prodlist,array('prod'=>"prod2",'pcs'=>2,'price'=>100));

createProducts($prodlist,10);

所以我的逻辑是这样的:

  • 使用createOffer函数创建引号。它返回新创建的报价ID
  • 然后你构建产品数组(我这里只有非常基本的数据)并通过引用引用的ID来添加它

也许不是最美丽的解决方案,但有效。

答案 1 :(得分:1)

也许你可以这样开始(根据你的参考链接)。

手动:https://wiki.vtiger.com/archives/index.php/vtiger510:Webservice_reference_manual
登录:https://wiki.vtiger.com/archives/index.php/vtiger510:Webservice_reference_manual#Login

伪;

<?php
class VTiger_Login
{
    private $serviceURL = 'http://vtiger_url/webservice.php?operation=login&username=%s&accessKey=%s';
    // A Vtiger username.
    private $userName = 'my_username';
    // An md5 of the concatenation of the challenge token and the user's webservice access key. 
    private accessKey = 'my_accesskey';

    public function login() {
        // Open CURL
        $ch = curl_init();
        // Set URL as same as on manual
        curl_setopt($ch, CURLOPT_URL, sprintf($this->serviceURL, $this->userName, $this->accessKey));
        // Need POST according to manual
        curl_setopt($ch, CURLOPT_POST, 1);
        // Receive server response = TRUE
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // Exec CURL
        $result = curl_exec($ch);
        // Close CURL
        curl_close($ch);

        /*
        $result should be like this according to manual;
        LoginResult {
            sessionId: String     // Unique Identifier for the session
            userId: String        // The vtiger id for the logged in user
            version: String       // The version of the webservices api
            vtigerVersion: String // The version of the vtiger crm.
        } 
        */

        // From manual: All structural data including response from the api is represented as JSON strings. 
        $result =@ json_decode($result);
        // See "Response" on manual
        if (null === $result) {
            throw new Exception('No response returned from Vtiger server!');
        }
        // See "ErrorObject" on manual
        if (null !== $result->success && false === $result->success) {
            throw new Exception('Something went wrong with login operation! errorCode: '. 
                        $result->errorCode .', errorMessage: '. $result->errorMessage);
        }

        // I think, there is no problem anymore, go with $result after this line...
    }
}