2013-07-24 42 views
3

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

我正在开发一个使用spring MVC(Restful API)的web应用程序。我想使用sugarcrm作为我的crm模块。我可以将两者都集成吗?

我已经通过糖的文档大致相同,但我不知道PHP的偏见编程。 任何人都可以帮我吗?

谢谢。

+0

“sugar.js”与“sugarcrm”无关, – Ven

回答

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; 
?>