webservice.php Vtiger update Query String php curl post

[亡魂溺海] 提交于 2019-12-02 06:35:12

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;
}

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.

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, '&');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!