SugarCRM 将 REST API 与自定义模块结合使用

SugarCRM Use REST API With Custom Module

我在 SugarCRM 中创建了一个自定义模块(我认为是 V7.8,托管,而不是内部部署)

该模块名为 "Customers",用于测试目的 - 我将结构与 "Accounts" 相同。

我正在使用此页面上的示例:http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/API/Web_Services/Examples/v10/module_POST/

当我运行 Accounts 模块的代码工作时。当我尝试 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>";

自定义模块将有一个前缀键以避免冲突,因此而不是 $url = $base_url . "/Customers"; 它会像 $base_url . "/xxx_Customers";.
如果你开发了模块,你应该知道使用的前缀键,否则检查数据库模式或 Studio/Module Builder

确保您的模块前缀,同时通过点击此 URL 检查您的端点方法以获得所需的 API 方法和参数:yourdomain/rest/v10/help.