3

I am trying to retrieve custom module data through the Sugarcrm REST api but I am not able to do so as I am not even able to login with the documentation code, I tried same thing as given in documentation

    <?php

// specify the REST web service to interact with
$url = 'localhost/~jmertic/sugarcrm/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' => 'username',
        'password' => md5('password'),
        ),
    );
$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;

changed the username,password and url to match my setup but i get an error stating

No direct script access allowed

I tried to search this on web but couldnt find any relevant solution. I am using sugarCRM 6.5.0RC2 version

Regards, Anand Joshi

Anand Joshi
  • 452
  • 7
  • 23

2 Answers2

0

Shouldn't the line be this...

$url = 'http://yoursugarinstance/service/v4_1/rest.php';
jmertic
  • 2,208
  • 1
  • 13
  • 8
  • ya, i had mentioned the same `code` "changed the username,password and url to match my setup but i get an error stating" `code` – Anand Joshi Jul 06 '12 at 17:22
0

You probably has some defense configured on your WEB server which allows you to access only to index.php.

To verify it, try to go from the browser to your API URL: http://YOUR_DOMAIN_NAME/service/v4_1/rest.php

Or/and run from terminal: wget http://YOUR_DOMAIN_NAME/service/v4_1/rest.php

If it shows the same message, check your .httaccess on this folder or/and your web server config file.

If no, how do you run the API test script? through CLI or from browser?

Also I suggest you to use some Open Source SugarCRM REST API Wrapper. I use this one: https://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class

Kostanos
  • 9,615
  • 4
  • 51
  • 65