2013-07-24 70 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; 
?>