Error using login method in REST API

Hi there

We are trying to use the API REST of uiteCRM 7 (versión 4.1), but we are not able even to make the “login” call to work

When we call this method in the API in the log file does not appear any error (when credentials are right), but the result from the REST call is empty

we have this code (Which we obtained directly from the wiki documentation about REST API):

<?php

require_once('../custom/constantes.php');

function restRequest($method, $arguments){

         $curl = curl_init(URL_REST);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         $post = array(
                              "method" => $method,
                       "input_type" => "JSON",
                       "response_type" => "JSON",
                       "rest_data" => json_encode($arguments),
        );

        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);

       $result = curl_exec($curl);
       curl_close($curl);
       return json_decode($result,1);
}

$userAuth = array(
             'user_name' => NAME_USER_PAY,
             'password' => md5(PASSWORD_USER_PAY),
 );
 $appName = 'My SuiteCRM REST Client';
 $nameValueList = array();

 $args = array(
               'user_auth' => $userAuth,
             'application_name' => $appName,
             'name_value_list' => $nameValueList);

 $result = restRequest('login',$args);
echo "Tamaño:".sizeof($result);
 $sessId = $result['id'];
 echo " LA SESION ES".$sessId;
?>

The issue is that when credentials are wrong, an error message in the log file appears saying “FAILED LOGIN:attempts[1]”, but when credentials are fine, the $result variable is empty, nothing is inside, and the Session Id does not appear

Any ideas please?

We have checked that URL and credentials are fine :blink:

Thanks a lot in advance

Hi there, sorry to ask agatin… but anyone that can give us a quick “push” to check this??

Have a look at https://github.com/daniel-samson/suitecrm-rest-client .

Hi djsamson, thanks a lot for your reply!

Yes we have used this code and we get the same behaviour. The only thing is that we needed to change this line of code in the library

 public function __construct($user, $pass, $host, $rest_api = "/service/v4_1/rest.php")
    {
        $this->rest_user = $user;
        $this->rest_pass = $pass;
        $this->base_url = 'http://' . preg_replace('~^http://~', '', $host);
        $this->rest_url = $host . $rest_api;
    }

by this

 public function __construct($user, $pass, $host, $rest_api = "/service/v4_1/rest.php")
    {
        $this->rest_user = $user;
        $this->rest_pass = md5($pass);
        $this->base_url = 'http://' . preg_replace('~^http://~', '', $host);
        $this->rest_url = $host . $rest_api;
    }

If we do not include the call to the “md5” function and we just pass the password, the login fails.

But again the $user and $pass are incorrect, we get an “authentication error” in the suitecrm.log file, but if the $user and $pass are fine, we get the $result for the login function set to null.

Is there any configuration of something extra we need to apply in SuiteCRM, php…?

We are quite stocked at this point, and we need to communicate a third Joomla system with SuiteCRM… our idea was to do it using REST API… but we are not able to get it.

Any idea about what could be the problem, or what to check in order to get a better idea of what can be happening???

Thanks a lot

Hi again

I think we found where the problem is. Seems to us that the json_decode function is not returning the data in the fight format

When you call the login() function you end in the restRequest($call_name, $call_arguments)

where we can find this code

private function restRequest($call_name, $call_arguments)
    {
        ob_start();
        $ch = curl_init();
        $post_data = 'method=' . $call_name . '&input_type=JSON&response_type=JSON';
        $jsonEncodedData = json_encode($call_arguments);
        echo "Encoded data:".$jsonEncodedData."<br/>";
        $post_data = $post_data . "&rest_data=" . $jsonEncodedData;
        curl_setopt($ch, CURLOPT_URL, $this->rest_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        echo "OUTPUT: ".$output . "<br/>";
        $response_data = json_decode($output, true);

         echo "Decoded data: ". $response_data."<br/>";
        curl_close($ch);
        ob_end_flush();
        return $response_data;
     
    }

When we make the first echo to show the encoded data we obtain this:

Then in the second echo, to try to show the ouput we obtain

but when we get to the json_decode function and try to store it in the $response_data, nothing is stored in this variable, it seems like the

 $response_data = json_decode($output, true);

returns an empty string

how can we solve it?