REST Api for Sugarcrm - rest

Sugarcrm is providing Restful API support.So how can i check json response using rest client(browser plugin to check restful web services)?.
I am developing a web-app using spring MVC(Restful API).I want to use sugarcrm as my crm module.how can i integrate both?.
I have gone through sugar's documentation about the same ,but I do not have any idea about php biased programming.
can anyone please help me?
Thanks.

run below code, if you have any problem then let me know.
<?php
//Put your Base url
$url = 'yoursugarcrm_url/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' => 'admin',
'password' => md5('uDje9ceUo89nBrM'),
),
);
$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;
//echo json_encode(array("sessionId"=>$sessionId));
//Your moduel parameter
//Parameter of the customer
$fullname = $_POST['fullname'];
$password = md5($_POST['password']);
$email_address = $_POST['email_address'];
// My moduel
$parameters = array(
'session' => $sessionId, //Session ID get from session.php
'module' => 'custo_Customers', // Your PackageKey_ModuleName
'name_value_list' => array (
array('name' => 'fullname', 'value' => $fullname),
array('name' => 'email_address', 'value' => $email_address),
array('name' => 'password', 'value' => $password),
),
);
$json = json_encode($parameters); // Json strgin
$postArgs = 'method=set_entry&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,true);
echo $response;
?>

Related

Upload Video to Facebook Page with call to action in php

I am trying to upload video to facebook page as page admin with the call to action:
// Post Data
$action_call_array = array('type' => 'LEARN_MORE', 'value' => array('link' => 'http://www.google.com'));
$data = [
'title' => 'Demo Title',
'description' => 'Demo Description
http://www.google.com
http://hk.yahoo.com',
'source' => new CURLFile('video/video_1mb.mp4', 'video/mp4'),
'call_to_action' => $action_call_array,
'access_token' => FACEBOOK_PAGE_TOKEN, // MUST BE INCLUDED !!!
];
// Post Url
$post_url = 'https://graph-video.facebook.com/'.FACEBOOK_PAGE_ID.'/videos';
// CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
// result
$j = json_decode($return);
$page_post_id = $j->id;
var_dump($return);
Without the call to action, it works. But after adding the call to action, it returns me the following error:
string(196) "{"error":{"type":"Exception","message":"No Call To Action Type was parseable. Please refer to the call to action api documentation","code":1373054,"is_transient":false,"fbtrace_id":"H9GksxYBdQx"}}"
Looks like the way I add the call to action is not correct. But I don't really know how to fix it.
I put the call to action into url parameter and it is working now!
// Post Data
$fb_action = 'LEARN_MORE';
$fb_action_link = 'http://www.google.com';
$action_call_array = array('type' => $fb_action, 'value' => array('link' => $fb_action_link));
$params = http_build_query(array('call_to_action' => $action_call_array));
$data = [
'title' => 'Demo Title',
'description' => 'Demo Description
http://www.google.com
http://hk.yahoo.com',
'source' => new CURLFile('video/video_1mb.mp4', 'video/mp4'),
'access_token' => FACEBOOK_PAGE_TOKEN, // MUST BE INCLUDED !!!
];
// Post Url
$post_url = 'https://graph-video.facebook.com/'.FACEBOOK_PAGE_ID.'/videos?'.urldecode($params);
// CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
// result
$j = json_decode($return);
$page_post_id = $j->id;

Drupal7 REST: I am not able to perform POST and PUT methods. Error is :Not Acceptable : Node type is required, Code:406?

I'm using drupal7. my drupal_http_request() for get and delete are working fine for authenticated users, but the post and put methods are not working.
The error is :Not Acceptable : Node type is required, and http error code is :406. My code is below:
function ws_form_post_auth() {
$base_url = 'http://localhost/drupalws/api/v1';
$data = array(
'username' => 'student1',
'password' => 'welcome',
);
$data = http_build_query($data, '', '&');
$options = array(
'headers' => array(
'Accept' => 'application/json',
),
'method' => 'POST',
'data' => $data
);
$response = drupal_http_request($base_url . '/user/login', $options);
$data = json_decode($response->data);
// Check if login was successful
if ($response->code == 200) {
$options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
$options['headers']['X-CSRF-Token'] = $data->token;
$data = array(
'title' => 'First forum post',
'type'=> 'forum',
'body'=> array(
'und'=>array(
0=> array(
'value'=>'This is my first forum post via httprequest.'
)
)
)
);
$data = json_encode($data);
$options['data'] = $data;
$options['method'] = 'POST';
$response = drupal_http_request($base_url . '/node', $options);
return $response->status_message;
}
return $response->status_message;
}
I got the solution for my issue,I just missed a Content-Type in Headers.
[....]
if ($response->code == 200) {
$options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
$options['headers']['X-CSRF-Token'] = $data->token;
$options['headers']['Content-Type'] = 'application/json';
[....]

how to get all accounts records including deleted records from sugarcrm using REST api

following is my code to get result of account module from sugarcrm using rest api
i want to fetch all records including deleted one but it only throws either deleted or non-deleted accounts. but i need both type of record using one request please help
$url = "http://localhost/SugarCRM/service/v4/rest.php";
$username = "admin";
$password = "1234";
//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);
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);
echo "</pre>";
*/
//get session id
$session_id = $login_result->id;
$current_date='2014-06-30';//date('Y-m-d');
//get list of records --------------------------------
$get_entry_list_parameters = array(
//session id
'session' => $session_id,
//The name of the module from which to retrieve records
'module_name' => 'Accounts',
//The SQL WHERE clause without the word "where".
'query' => "(accounts.deleted='0' or accounts.deleted='1') AND DATE(accounts.date_entered)='$current_date'",
//The SQL ORDER BY clause without the phrase "order by".
'order_by' => "",
//The record offset from which to start.
'offset' => '0',
//Optional. A list of fields to include in the results.
'select_fields' => array(
'id',
'name',
'billing_address_street',
'billing_address_city',
'billing_address_postalcode',
'billing_address_country',
'shipping_address_street',
'shipping_address_city',
'shipping_address_postalcode',
'shipping_address_country',
'website',
'deleted',
),
/*
A list of link names and the fields to be returned for each link name.
Example: 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
*/
'link_name_to_fields_array' => array(
),
//The maximum number of results to return.
'max_results' => '',
//To exclude deleted records
//'deleted' => false,
//If only records marked as favorites should be returned.
//'Favorites' => false,
);
$get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url);
echo '<pre>';
print_r($get_entry_list_result);
echo '</pre>';
I would suggest not using the query section, just uncomment and set 'deleted' => true, I am using version 4_1 of the API and that works for me.
It's likely that the default is false and your query is being overridden by it.
Hope that helps

SugarCRM REST API how to get the list of teams of the module

Is there any way to get the teams list which the current module record belongs to by using the REST API of SugarCRM Pro?
I already tried the get_entry_list by passing the relationship array with 'Teams' - this returns the empty array.
It should be the similar function to get_user_team_id but for specific module.
I also tried get_relationships method, but also no luck.
My guess because the Teams relationship is the Link clas, not the Link2 as all other modules.
In order to retrieve the teams I believe you will need to do the following:
1) Retrieve your records using get_entry_list making sure to return the team_set_id field.
2) Once you have the team_set_ids, you can do another get_entry_list call to the module 'TeamSets' filtering those ids in the query parameter. You can then use the 'link_name_to_fields_array' parameter to return the individual teams on a team set.
An Example is below
<?php
$url = "http://sugar_url/service/v4_1/rest.php";
$username = "admin";
$password = "password";
//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);
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);
echo "</pre>";
*/
//get session id
$session_id = $login_result->id;
//get list of records --------------------------------
$get_entry_list_parameters = array(
//session id
'session' => $session_id,
//The name of the module from which to retrieve records
'module_name' => 'TeamSets',
//The SQL WHERE clause without the word "where".
'query' => "",
//The SQL ORDER BY clause without the phrase "order by".
'order_by' => "",
//The record offset from which to start.
'offset' => '0',
//Optional. A list of fields to include in the results.
'select_fields' => array(
'id',
),
/*
A list of link names and the fields to be returned for each link name.
Example: 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
*/
'link_name_to_fields_array' => array(
array(
'name' => 'teams',
'value' => array(
'id',
'name'
),
),
),
//The maximum number of results to return.
'max_results' => '100',
//To exclude deleted records
'deleted' => '0',
//If only records marked as favorites should be returned.
'Favorites' => false,
);
$get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url);
echo '<pre>';
print_r($get_entry_list_result);
echo '</pre>';
?>
Yes. Specify the team_name field in your request:
https://xxx.sugarondemand.com/rest/v10/Accounts/7e785902-ff39-4a54-87a4-e0d38b6b6ed1?&fields=name,team_name
Response:
{
"id": "7e785902-ff39-4a54-87a4-e0d38b6b6ed1",
"name": "15527-1",
"date_modified": "2016-11-28T12:05:17-05:00",
"team_name": [
{
"id": "5272c830-5339-11e6-9195-06ef03a1f4b3",
"name": "Team1",
"name_2": "",
"primary": true
},
{
"id": "370d8fe4-e431-79fb-ec42-5565ecba3187",
"name": "Web Service",
"name_2": "CRM",
"primary": false
}
],
"_acl": {"fields": {}},
"_module": "Accounts"
}

facebook api returning "Got EOF while waiting for outstanding responses" error message

I am posting images through my app to users wall on their behalf (they step in front of a camera at events). MY app was working successfully last week now its not working and reporting "Got EOF while waiting for outstanding responses".
There is a related bug report but its for an iphone app while mine is a web based php.
Can anyone shed light on this error message? my code is below but its broken down a lot for pic processing and db calls..
//initial calling code
$data = array('title' => $station_info->title,
'message' => $station_info->message,
'token' => $result['access_token'],
'item' => $item,
'pic_source' => $image,
'fb_id' => $result['fb_id'],
'album_id' => $station_info->pic_station_album_id,
'user_id' => $user_id_data['id'],
);
//$result = $this->__do_facebook_image_post($data);
$feedback = "FACEBOOKIMG: " . $this->__do_facebook_image_post($data, 'x');
///processing code
private function __do_facebook_image_post($data,$type = "normal"){
$attachment = array(
'title' => $data['title'],
'message' => $data['message'],
'token' => $data['token']
);
if($type =="normal"){
$folder = "webcam";
}else if($type="x"){
$folder = "x";
}
$upload_path = FCPATH . "/pic_uploads/".$folder."/".$data['item'];
echo $upload_path;
if(is_file($upload_path))
{
$attachment['source'] = '#' . realpath(FCPATH . "/img_uploads/".$folder."/". $upload_path);
}
else
{
$attachment['source'] = '#' . realpath(FCPATH . "/img_uploads/".$folder."/". $data['pic_source']);
}
return $this->facebook_img_post($data['fb_id'],$attachment,$this->__get_facebook_album_id($data['album_id'],$data['user_id'],$data['token']),$data['token']);
}
private function __do_facebook_image_post($data,$type = "normal"){
$attachment = array(
'title' => $data['title'],
'message' => $data['message'],
'token' => $data['token']
);
if($type =="normal"){
$folder = "webcam";
}else if($type="x"){
$folder = "x";
}
$upload_path = FCPATH . "/pic_uploads/".$folder."/".$data['item'];
echo $upload_path;
if(is_file($upload_path))
{
$attachment['source'] = '#' . realpath(FCPATH . "/img_uploads/".$folder."/". $upload_path);
}
else
{
$attachment['source'] = '#' . realpath(FCPATH . "/img_uploads/".$folder."/". $data['pic_source']);
}
return $this->facebook_img_post($data['fb_id'],$attachment,$this->__get_facebook_album_id($data['album_id'],$data['user_id'],$data['token']),$data['token']);
}
private function __post_to_facebook($url,$attachment)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // This i added as the URL is https
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // This i added as the URL is https
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, true); // Do I need this ?
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result= curl_exec($ch);
curl_close ($ch);
return $result;
}
function facebook_img_post($fb_id,$data,$album_id ='')
{
if(!empty($album_id)){
$url = "https://graph.facebook.com/".$album_id."/photos";
}else{
$url = "https://graph.facebook.com/".$fb_id."/photos";
}
$attachment = array(
'access_token' => $data['token'],
'message'=> $data['message'],
'source' => $data['source'] . ";type=".get_mime_by_extension($data['source'])
);
return $this->__post_to_facebook($url,$attachment);
}
The error message that got returned didn't really match the problem.
I fixed this error as I was trying to upload a photo but was not passing the correct file path.