2016-12-08 53 views
0

我在SugarCRM的創建自定義模塊(V7.8我認爲,託管,而不是前提)使用SugarCRM的REST API使用自定義模塊

模塊被命名爲「客戶」,並且是用於測試目的 - 我做了該結構與「賬戶」相同。

我使用這個頁面上的例子:http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/API/Web_Services/Examples/v10/module_POST/

當我運行代碼的帳戶模塊它的工作原理。當我嘗試發佈記錄的客戶模塊我得到:

[error] => no_method 
[error_message] => Could not find a route with 1 elements 

我的理解是,這些API都應該對自定義模塊的工作,這是不正確的?我的最終目標是創建5或6個定製模塊,並通過REST API實時從我們的ERP系統中將數據推送到它們中。

這裏的示例代碼:

<?php 
$base_url = "https://my.domain/rest/v10"; 
$username = "admin"; 
$password = "*********"; 
function call($url,$oauthtoken='',$type='GET',$arguments=array(),$encodeData=true,$returnHeaders=false){ 
    $type = strtoupper($type); 
    if ($type == 'GET') 
    { 
     $url .= "?" . http_build_query($arguments); 
    } 
    $curl_request = curl_init($url); 
    if ($type == 'POST') 
    { 
     curl_setopt($curl_request, CURLOPT_POST, 1); 
    } 
    elseif ($type == 'PUT') 
    { 
     curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT"); 
    } 
    elseif ($type == 'DELETE') 
    { 
     curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE"); 
    } 
    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 
    curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders); 
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0); 
    if (!empty($oauthtoken)) 
    { 
     $token = array("oauth-token: {$oauthtoken}","Content-Type: application/json"); 
     curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token); 
    } 
    if (!empty($arguments) && $type !== 'GET') 
    { 
     if ($encodeData) 
     { 
      //encode the arguments as JSON 
      $arguments = json_encode($arguments); 
     } 
     curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments); 
    } 
    $result = curl_exec($curl_request); 
    if ($returnHeaders) 
    { 
     //set headers from response 
     list($headers, $content) = explode("\r\n\r\n", $result ,2); 
     foreach (explode("\r\n",$headers) as $header) 
     { 
      header($header); 
     } 
     //return the nonheader data 
     return trim($content); 
    } 
    curl_close($curl_request); 
    //decode the response from JSON 
    $response = json_decode($result); 
    return $response; 
} 

//Login - POST /oauth2/token 
$url = $base_url . "/oauth2/token"; 
$oauth2_token_arguments = array(
    "grant_type" => "password", 
    //client id/secret you created in Admin > OAuth Keys 
    "client_id" => "sugar", 
    "client_secret" => "", 
    "username" => $username, 
    "password" => $password, 
    "platform" => "base" 
); 
$oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments); 
//Create record - POST /<module>/ 
$url = $base_url . "/Customers"; //works id this is "Accounts" 
$record_arguments = array(
    "name" => "ACME Inc.", 
    "description" => "Not for Coyotes" 
); 
$record_response = call($url, $oauth2_token_response->access_token, 'POST', $record_arguments); 
echo "<pre>"; 
print_r($record_response); 
echo "</pre>"; 

回答

1

自定義模塊,將有一個前綴鍵,以避免衝突,所以不是$url = $base_url . "/Customers";它將會像$base_url . "/xxx_Customers";
如果您開發了該模塊,您應該知道使用的前綴密鑰,否則請檢查數據庫模式或Studio/Module Builder

+0

我回去搜索了文檔,發現:「密鑰:密鑰是字母數字文本來區分具有相似名稱的模塊,系統會用這個鍵爲所有類名,目錄和表名添加前綴。「這原來是你表示的前綴。我認爲文件可能會更清楚一點。感謝您及時的回覆。 –