3

Sugarcrm 正在提供 Restful API 支持。那么我如何使用 rest 客户端检查 json 响应(浏览器插件来检查 restful web 服务)?

我正在使用spring MVC(Restful API)开发一个网络应用程序。我想使用sugarcrm作为我的crm模块。我怎样才能集成两者?。

我已经阅读了有关相同内容的 Sugar 文档,但我对 php 偏向编程一无所知。谁能帮帮我吗?

谢谢。

4

1 回答 1

1

在下面运行代码,如果您有任何问题,请告诉我。

<?php 
//Put your Base url 
    $url = 'yoursugarcrm_url/service/v4_1/rest.php';
    // Open a curl session for making the call
    $curl = curl_init($url);
    // Tell curl to use HTTP POST
    curl_setopt($curl, CURLOPT_POST, true);
    // Tell curl not to return headers, but do return the response
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // Set the POST arguments to pass to the Sugar server
    $parameters = array(
                    'user_auth' => array(
                        'user_name'     => 'admin',
                        'password'      => md5('uDje9ceUo89nBrM'),
                        ),
    );
    $json = json_encode($parameters);
    $postArgs = array(
        'method'        => 'login',
        'input_type'    => 'JSON',
        'response_type' => 'JSON',
        'rest_data'     => $json,
    );
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
    // Make the REST call, returning the result
    $response = curl_exec($curl);
    // Convert the result from JSON format to a PHP array
    $result = json_decode($response);
    if ( !is_object($result) ) {
        die("Error handling result.\n");
    }
    if ( !isset($result->id) ) {
        die("Error: {$result->name} - {$result->description}\n.");
    }
    // Get the session id 
    $sessionId = $result->id;
    //echo  json_encode(array("sessionId"=>$sessionId));


//Your moduel parameter
//Parameter of the customer
    $fullname       = $_POST['fullname'];         
    $password       = md5($_POST['password']);
    $email_address  = $_POST['email_address'];

    // My moduel     
    $parameters = array(
        'session' => $sessionId, //Session ID   get from session.php
        'module' => 'custo_Customers', // Your PackageKey_ModuleName   
                'name_value_list' => array ( 
                      array('name' => 'fullname', 'value' => $fullname), 

                      array('name' => 'email_address', 'value' => $email_address), 
                      array('name' => 'password', 'value'      => $password), 
               ), 
        ); 

    $json       = json_encode($parameters);   // Json strgin
    $postArgs   = 'method=set_entry&input_type=JSON&response_type=JSON&rest_data=' . $json;
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

    // Make the REST call, returning the result 
    $response   = curl_exec($curl);
    // Convert the result from JSON format to a PHP array 
    $result     = json_decode($response,true); 
echo $response;
?>
于 2015-04-27T14:28:01.930 回答