2016-08-19 32 views
1

是否有人知道如何正確格式化vtiger中的更新查詢以更新Lead模塊下的記錄?webservice.php Vtiger更新查詢字符串php curl post

我一直在關注這一點:http://community.vtiger.com/help/vtigercrm/developers/third-party-app-integration.html

,並已能夠登錄,查詢,並接受了這種挑戰的反應,但我一直無法得到更新功能工作,它可能是因爲我不是確定他們希望查詢的外觀。這是我的錯誤,當我發送查詢:

stdClass Object ([success] => [error] => stdClass Object ([code] => ACCESS_DENIED [message] => Permission to perform the operation is denied for id)) 

電流測試代碼:

function updatesomeone(){ 
global $createduserleadnum; 
global $url; 
global $sessionID; 
global $createduserid; 


$customdata = array(
'firstname'=> 'TestAPILead2',//Update First name 
'lastname'=> 'TestAPILeadLast2', //Updated Last name 
'leadstatus'=> 'New', 
'leadsource'=> 'Some Lead Source', //Not Real Lead source 
'assigned_user_id'=> 'User-Assigned', //not real user 
'cf_755'=> 'A Custom Field', // A Custom Field 
'lead_no' => $createduserleadnum, Acquired from other function/stored value 
); 

$customdata = json_encode($customdata); 
$field = array(
'operation' => 'update', 
'sessionName'=> $sessionID, 
'element' => $customdata 
); 


$fields_string; 
foreach($field as $key=>$value) { global $fields_string; 
$fields_string .= $key.'='.$value.'&'; } 
$ch = curl_init(); 


curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch,CURLOPT_POST, count($field)); 
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

$result = curl_exec($ch); 
$pringjson = json_decode($result); 

print_r($pringjson); 

}

回答

1

想通了。它與$ fieldstring變量有關。出於某種原因,它並沒有侷限於該函數,所以它包含了一些其他變量。只是在最後用一個數字改變了字符串變量。在最後的代碼中,我會爲url編寫一個更好的腳本 - ify變量。我也必須使用完整的ID。無論哪種方式,它現在已經解決了,代碼正常工作。

0

我對你的代碼有一個建議。您還沒有刪除&,在「foreach」循環後會生成該文件的末尾。所以只需在foreach之後添加rtrim並將$ fields_string變量定義爲空白。

$fields_string = ''; 
foreach($field as $key=>$value) { 
    global $fields_string; 
    $fields_string .= $key.'='.$value.'&'; 
} 
rtrim($fields_string, '&'); 
2

API的文檔不是關於對象的更新那麼清楚,所以我雖然這段代碼可能是一些你們感興趣。

基本上,能夠更新一個對象,你至少需要

  1. 的ID
  2. 對象的所有「必需」字段更新(取決於對象類型的),即使你不想更新它們。例如,對於標準配置中的Lead,您至少需要「Last name」和「Assigned User ID」。

這裏是我的代碼,如果需要的話:

$vt_address = 'URL of Vtiger install'; 
$vt_session = 'Vtiger Session ID'; 

$object_to_update = array('id' => '10x71', 'lastname'=>'Test Lead', 'assigned_user_id' => '19x1'); 

vtlib_update_data($vt_address, $vt_session, $object_to_update); 

function vtlib_update_data($vt_address, $vt_session, $vt_object) { 

    $vt_json = json_encode($vt_object); 
    $vt_url = "$vt_address/webservice.php"; 

    $vt_post_data = array(
     'operation' => 'update', 
     'sessionName' => $vt_session, 
     'element' => $vt_json 
    ); 

    $vt_data = vtlib_curl($vt_url, $vt_post_data); 

    return ($vt_data['success']) ? $vt_data['result'] : false; 

} 

function vtlib_curl($vt_url, $vt_post_data) { 

    $vt = curl_init($vt_url); 
    curl_setopt($vt, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($vt, CURLOPT_POST, true); 
    curl_setopt($vt, CURLOPT_POSTFIELDS, $vt_post_data); 
    $vt_data = json_decode(curl_exec($vt), true); 
    curl_close($vt); 

    return $vt_data; 
}