How to Consume api Rest V4.1

I need to create an api for case registration
I’m following the developer guide but I can’t understand very well
I have this code where I put the endpoint and login

<?php
$url = "http://192.168.0.96/service/v4_1/rest.php";
$username = "mateo.morales";
$password = "123";
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);
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" => "1"
 ),
 "application_name" => "RestTest",
 "name_value_list" => array(),);
 $login_result = call("login", $login_parameters, $url);
  /*
 echo "<pre>";
print_r($login_result); //nothing
echo "</pre>";
*/
//get session id
$session_id = $login_result->id;
//retrieve fields -------------------------------- 
$get_module_fields_parameters = array(
 //session id
 'session' => $session_id,
 //The name of the module from which to retrieve records
 'module_name' => 'Accounts',
 //Optional. Returns vardefs for the specified fields. An empty array will return all fields.
 'fields' => array(
     'id',
     'name',
 ),
);
$get_module_fields_result = call("get_module_fields", $get_module_fields_parameters, $url);
echo "<pre>";
print_r($get_module_fields_result); //nothing again
echo "</pre>";

but I don’t know how to execute that file that contains the api structure

Can you try this

<?php

$url = "http://192.168.0.96/service/v4_1/rest.php";

function restRequest($method, $arguments){
 global $url;
 $curl = curl_init($url);
 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' => 'mateo.morales',
    'password' => md5('123'),
);

$appName = 'Rest APP to CRM';
$nameValueList = array();

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

$result = restRequest('login',$args);
$sessId = $result['id'];

$entryArgs = array(
 //Session id - retrieved from login call
	'session' => $sessId,
 //Module to get_entry_list for
	'module_name' => 'Accounts', 
 //Filter query - Added to the SQL where clause,
	'query' => "accounts.billing_address_state='Florida'",
 //Order by - unused
	'order_by' => '',
 //Start with the first record
	'offset' => 0,
 //Return the id and name fields
	'select_fields' => array('id','name',),

   //Show 10 max results
  		'max_results' => 10,
   //Do not show deleted
  		'deleted' => 0,
 );

$result = restRequest('get_entry_list',$entryArgs);
echo "<pre>";
print_r($result);
1 Like