2017-07-26 18 views
0

我有代碼連接到vtiger,現在我有問題,插入數據到vtiger模塊帳戶。選擇語句我沒有問題,它工作正常,但我不能插入數據。我想創建新的組織。插入vtiger表,php

function call($url, $params, $type = "GET") 
{ 
    $is_post = 0; 
    if($type == "POST") { 
     $is_post = 1; 
     $post_data = $params; 
    } else { 
     $url = $url . "?" . http_build_query($params); 
    } 
    $ch = curl_init($url); 
    if(!$ch) { 
     die("Cannot allocate a new PHP-CURL handle"); 
    } 
    if($is_post) { 
     curl_setopt($ch, CURLOPT_POST, $is_post); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); 
    } 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $data = curl_exec($ch); 

    $return = null; 
    if(curl_error($ch)) { 
     $return = false; 
    } else { 
     $return = json_decode($data, true); 
    } 

    curl_close($ch); 

    return $return; 
} 

$endpointUrl = 'http://localhost/vtigercrm/webservice.php'; 
$userName = 'username'; 
$password = 'pass'; 
$userAccessKey = 'acckey'; 

$sessionData = call($endpointUrl, array(
    "operation" => "getchallenge", 
    "username" => $userName 
)); 
$challengeToken = $sessionData['result']['token']; 
$generatedKey = md5($challengeToken . $userAccessKey); 
$dataDetails = call($endpointUrl, array(
    "operation" => "login", 
    "username" => $userName, 
    "accessKey" => $generatedKey 
), "POST"); 
$query = "INSERT INTO Accounts(accountname) VALUES ('some_name');"; 
$sessionid = $dataDetails['result']['sessionName']; 
$getUserDetail = call($endpointUrl, array(
    "operation" => "query", 
    "sessionName" => $sessionid, 
    'query' => $query 
)); 
+0

歡迎堆棧溢出!尋求調試幫助的問題(「**爲什麼不是這個代碼工作?」)必須包括期望的行爲,特定的問題或錯誤以及在問題本身**中重現**的最短代碼。沒有**明確問題陳述**的問題對其他讀者無用。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – GrumpyCrouton

回答

0

要使用Web服務創建一個對象,你應該使用「創建」操作而不是「查詢」

您可以檢查Web服務教程:

https://wiki.vtiger.com/index.php/Webservices_tutorials#Create

+0

我嘗試創建但仍然沒有成功。 $ updateTable = call($ endpointUrl,array(「operation」=>「create」,「sessionName」=> $ sessionid,「element」=>「URLENCODED(」。$ query。「)」,「elementType」=> 「賬戶」),「POST」); –

+0

您的元素/參數必須是JSON格式 –

+0

我已經成功了,謝謝你的幫助。 –