REST API cant generate session ID

Hello,

I have problems in rest api, it doesn’t seems to generate a session id when I try to authenticate my self in my rest script. Is there anything that I need to know?

Do you have any recommendations?

I have worked with sugarcrm pro 6.5 and sugarcrm 7 with REST api before with no problem.

Thank you and Regards

Hi,

can you post your code here ? You have many sample on the net.
And is your suitecrm is on http or https ? very important.

Regards

below is my code.

$url = ‘https://myurl/SuiteCRM/service/v4_1/rest.php’;

  $username = "myusername";
$password = "mypassword";

//function to make cURL request
function call($method, $parameters, $url)
{
    ob_start();
    $curl_request = curl_init();

    curl_setopt($curl_request, CURLOPT_URL, $url);
    curl_setopt($curl_request, CURLOPT_POST, 1);
    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl_request, CURLOPT_HEADER, 1);
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

    $jsonEncodedData = json_encode($parameters);

    $post = array(
         "method" => $method,
         "input_type" => "JSON",
         "response_type" => "JSON",
         "rest_data" => $jsonEncodedData
    );

    curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
    $result = curl_exec($curl_request);
    $http_status = curl_getinfo($curl_request, CURLINFO_HTTP_CODE);
   print_r($http_status);
    print_r($result);
    curl_close($curl_request);

    $result = explode("\r\n\r\n", $result, 2);
    $response = json_decode($result[1]);
    ob_end_flush();

    return $response;
}

//login ------------------------------ 
$login_parameters = array(
     "user_auth" => array(
          "user_name" => $username,
          "password" => md5($password),
          "version" => "4"
     ),
     "application_name" => "RestTest",
     "name_value_list" => array(),
);

$login_result = call("login", $login_parameters, $url);

echo "<pre>";
print_r($login_result);
echo "</pre>";

//get session id
$session_id = $login_result->id;

echo $session_id;

is there something wrong with that?

and my suittecrm is on https.

Thanks for your input.

Hi,
Here a working sample for HTTPS


	function context_call($method, $parameters, $url){

    	 $jsonEncodedData = json_encode($parameters);
    	 	// http_build_query
        	$post = http_build_query(array(
            	"method" => $method,
            	"input_type" => "JSON",
             	"response_type" => "JSON",
             	"track_view" => "0",
             	"application" => "front-end",
             	"rest_data" => $jsonEncodedData,
        	));

			$opts = array(
			  'http' => array(
			    'method'  => 'POST',
			    'header'  => 'Content-type: application/x-www-form-urlencoded',
			    'content' => $post
			  )
			);
			$context  = stream_context_create($opts);
			$result = file_get_contents($url, false, $context);
			return json_decode($result);

	}


You simple call this function for all method (login/set_entry …).

Regards

Hi,

Your sample code works to a demo suitecrm instance when I try to rest it, but in my suitecrm I still didn’t get a response.

Is there any other settings that I need to set here in suiteCRM? I know in sugar there’s no need of something.

Regards

Hi,

My code work on all situation ! I have loose many hours for debug why “sample code from sugar” don’t work on https !
As i know, your problem come another part…

  • version of suitecrm
  • root_of_crm/services/ is there a v4_1 folder ?
  • htaccess ?
  • apache config and log.

you must see in suitecrm log whats append ?

Regards

I got these ff errors.

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in /home/myscripthere.php on line 192

PHP Warning: file_get_contents(): Failed to enable crypto in /home/myscripthere.php on line 192

PHP Warning: file_get_contents(https://www.mysiteurl.com/SuiteCRM/service/v4_1/rest.php): failed to open stream: operation failed in /home/myscripthere.php on line 192

im using suitecrm 7.5.3
yes there is a service/4_1 folder
no htacess

what is this file
failed to open stream: operation failed in /home/myscripthere.php on line 192

You must write HTTPS in URL

You have more a Apache SSL config error than the script.

that is where I put your recommended script and also I included the https on the url.

thanks for your input

Try

curl --insecure https://blabla/service/v4_1/rest.php -vv => you must receive the content of rest.php
curl -https://blabla/ -vv => you must receive the login page

Try with – insecure

The result of these request give the problem you have.

hi here it is what I find.

what could be the problem here?

SSL issue? or apache issue?

see attached

where would i place this code?

what is this myurl is it localhost or domain name can youn tell me