webservice.php Vtiger update Query String php curl

2019-03-01 08:05发布

Does any one know how to properly format the update query in vtiger to update a record under the Leads module?

I have been following this: http://community.vtiger.com/help/vtigercrm/developers/third-party-app-integration.html

and have been able to login, query, and do the challenge response, but I have been unable to get the update function to work and it could be because I am not sure how they want the query to look. This is the error I get when I send the query:

stdClass Object ( [success] => [error] => stdClass Object ( [code] => ACCESS_DENIED [message] => Permission to perform the operation is denied for id ) )   

Current Test Code:

function updatesomeone(){
global $createduserleadnum;
global $url;
global $sessionID;
global $createduserid;


$customdata = array(
'firstname'=> 'TestAPILead2',//Update First name
'lastname'=> 'TestAPILeadLast2', //Updated Last name
'leadstatus'=> 'New',
'leadsource'=> 'Some Lead Source', //Not Real Lead source
'assigned_user_id'=> 'User-Assigned', //not real user
'cf_755'=> 'A Custom Field', // A Custom Field
'lead_no' => $createduserleadnum, Acquired from other function/stored value
);

$customdata = json_encode($customdata);
$field = array(
'operation' => 'update',
'sessionName'=> $sessionID,
'element' => $customdata
);


$fields_string;
foreach($field as $key=>$value) { global $fields_string;
$fields_string .= $key.'='.$value.'&'; }
$ch = curl_init();


curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($field));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($ch);
$pringjson = json_decode($result);

print_r($pringjson);

}

3条回答
Summer. ? 凉城
2楼-- · 2019-03-01 08:23

Figured it out. It was related to the $fieldstring variable. For some reason it was not staying local to the function so it was including some other variables. just changed the fieldstring variable with a digit at the end. In the final code I will write a better script for url-ify'ing the variables. I also had to use the full id given. Either way it was resolved now and the code works as it should.

查看更多
放荡不羁爱自由
3楼-- · 2019-03-01 08:32

I have a suggestion for your code. You have not remove & at the end of which will get generated after "foreach" loop. So just add rtrim after foreach and define your $fields_string variable as blank.

$fields_string = '';
foreach($field as $key=>$value) {
     global $fields_string;
     $fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string, '&');
查看更多
何必那么认真
4楼-- · 2019-03-01 08:34

The documentation of the api is not so clear regarding the update of an object, so I though this piece of code might be of interest for some of you guys.

Basically, to be able to update an object, you need at least

  1. the id
  2. all the "required" fields of the object to update (depending of the object type), even if you don't want to update them. For example for a Lead in the standard config, you need at least "Last name" and "Assigned User ID".

Here is my code if needed:

$vt_address = 'URL of Vtiger install';
$vt_session = 'Vtiger Session ID';

$object_to_update = array('id' => '10x71', 'lastname'=>'Test Lead', 'assigned_user_id' => '19x1');

vtlib_update_data($vt_address, $vt_session, $object_to_update);

function vtlib_update_data($vt_address, $vt_session, $vt_object) {

    $vt_json = json_encode($vt_object);
    $vt_url = "$vt_address/webservice.php";

    $vt_post_data = array(
        'operation' => 'update',
        'sessionName' => $vt_session,
        'element' => $vt_json
    );

    $vt_data = vtlib_curl($vt_url, $vt_post_data);

    return ($vt_data['success']) ? $vt_data['result'] : false;

}

function vtlib_curl($vt_url, $vt_post_data) {

    $vt = curl_init($vt_url);
    curl_setopt($vt, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($vt, CURLOPT_POST, true);
    curl_setopt($vt, CURLOPT_POSTFIELDS, $vt_post_data);
    $vt_data = json_decode(curl_exec($vt), true);
    curl_close($vt);

    return $vt_data;
}
查看更多
登录 后发表回答