Skip to content

Instantly share code, notes, and snippets.

@laszlocsontos
Created March 30, 2017 10:06
Show Gist options
  • Save laszlocsontos/6364274f69e09ee3563642aeb598c9e0 to your computer and use it in GitHub Desktop.
Save laszlocsontos/6364274f69e09ee3563642aeb598c9e0 to your computer and use it in GitHub Desktop.
Test PHP script for calling login method on SuiteCRM's REST API
<?php
$url = "http://{hostname}/service/v4_1/rest.php";
$username = "username";
$password = "password";
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);
$parameters = array(
"user_auth" => array(
"user_name" => $username,
"password" => md5($password),
"version" => "1"
),
"application_name" => "RestTest",
"name_value_list" => array(),
);
$jsonEncodedData = json_encode($parameters);
$post = array(
"method" => 'login',
"input_type" => "JSON",
"response_type" => "JSON",
"rest_data" => $jsonEncodedData
);
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($curl_request);
curl_close($curl_request);
$result = explode("\r\n\r\n", $result, 2);
$response = json_decode($result[1]);
ob_end_flush();
$session_id = $response->id;
print($session_id);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment