SugarCRM在自定义模块中使用REST API

时间:2016-12-08 03:12:09

标签: php rest sugarcrm

我在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模块的代码时,它可以工作。当我尝试将记录发布到Customers模块时,我得到:

[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>";

2 个答案:

答案 0 :(得分:1)

自定义模块将有一个前缀键以避免冲突,因此$url = $base_url . "/Customers";不是$base_url . "/xxx_Customers";,而是if let userEmail = UserEmail.text, !userEmail.isEmpty, let userPassword = UserPassword.text, !userPassword.isEmpty, let userRepeatPassword = UserRepeatPassword.text, !userRepeatPassword.isEmpty { ... } 。 如果您开发了模块,则应该知道使用的前缀密钥,否则请检查数据库模式或Studio / Module Builder

答案 1 :(得分:-1)

确保模块的前缀,还通过单击此URL以获得必需的API方法和参数$subscription = "Subscription A" Login-AzureRmAccount -Subscription $subscription Get-AzureRmSubscription Select-AzureRmSubscription -Subscription $subscription $resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName $server = Get-AzureRmSqlServer -ResourceGroupName $resourceGroup.ResourceGroupName -ServerName $serverName $database = Get-AzureRmSqlDatabase -ServerName $server.ServerName -ResourceGroupName $resourceGroup.ResourceGroupName -DatabaseName $databaseName New-AzureRmSqlDatabaseRestorePoint -RestorePointLabel $restorePointName -ResourceGroupName $resourceGroup.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName 来检查端点方法。