如何从vTiger CRM获取联系人到自定义PHP应用程序?

时间:2015-12-26 14:21:19

标签: php vtiger

我想从类似cron的脚本中获取联系人详细信息等信息并放入我自己的自定义PHP应用程序中。我发现他们确实提供了API但找不到用于获取联系人的API。

1 个答案:

答案 0 :(得分:0)

嗯首先你必须通过authenticaton进程首先得到一个令牌,我的项目是用codeigniter编写的,所以你必须去掉codeigniter函数

public function get_token()
{
    $CI = &get_instance();
    $service_url = $CI->config->item('crm_base_url')."/webservice.php?operation=getchallenge&username=".$CI->config->item('username')."";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$service_url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    curl_setopt($ch, CURLOPT_ENCODING,  '');
    $data = json_decode(curl_exec($ch));

    if (!empty($data->result->token))
    {
        $token = $data->result->token; 
    }
    else
    {
        $token = '';
    }

    return $token;
}

然后这个

    $CI = &get_instance();
    $CI->lang->load('rest_controller', 'english');
    $CI->load->model('User_model', true);

    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);

    $user = '';
    $pass = '';

    if (!empty($request))
    {
        foreach ($request as $key => $value) 
        {
            $user = $value->username;
            $pass = $value->password;
        }
    }


    if (empty($user) || (empty($pass) )) 
    { 

        $response = array('status' => FALSE, 'message' => 'username or password field cannot be null');  

    } 

    $sess = $CI->session->userdata('login_user');
    if (isset($sess)) 
    {
        $session_data['login_user'] = ''; 

        $CI->session->set_userdata($session_data['login_user']);
    }

    $errorMessage = '';

    $token = $this->get_token();
    $accessKey = $token.$this->config->item('accessKey');
    $encrypted = md5($accessKey);
    $pword = $encrypted;

    if (!empty($token))
    {
        $service_url = $this->config->item('crm_base_url')."/webservice.php";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$service_url);
        $curl_post_data = array(
                                'operation' => 'login',
                                'username' => $this->config->item('username'),
                                'accessKey' =>  $pword
                                );

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
        curl_setopt($curl, CURLOPT_ENCODING,  '');
        $curl_response = json_decode(curl_exec($curl));

        if ($curl_response->success !== FALSE)
        {
            $curl_response->result->uname = $user;
            $curl_response->result->accessKey = $pword;
            $this->set_user_session($curl_response->result);
            //$auth = $this->user_model->auth($user, $password);

            $query = urlencode("SELECT id, email, portal, cf_1253, firstname, lastname, account_id FROM Contacts WHERE email = '$user' AND cf_1253 = '$pass' AND portal = '1' ;");
            $service_url = $this->config->item('crm_base_url')."/webservice.php?operation=query&sessionName=".$CI->session->userdata('sessionName')."&query=".$query."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$service_url);
            curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_ENCODING,  '');
            $data_closed = json_decode(curl_exec($ch));
            $data_closed = object_to_array($data_closed);

            if (!empty($data_closed['result']))
            {
                $auth =  $data_closed['result'];
            }
            else
            {
                $auth =  FALSE;
            }

            if (!empty($auth))
            {
                $data = array(
                            'uid' => $auth[0]['id'],
                            'name' => $auth[0]['email'],
                            'firstname' => $auth[0]['firstname'],
                            'lastname' => $auth[0]['lastname'],
                            'login_user' => $auth[0]['email'],
                            'organization' => $auth[0]['account_id']
                            );

                $session_data = $CI->session->set_userdata($data);

                $response = array('status' => 'success',$session_data, 'message' => 'Logged in successfully.');
            }
            else
            {
                $response = array('status' => FALSE, 'message' => 'Invalid credentials') ;
            }
        }
        else
        {
            $response = array('status' => FALSE, 'message' => $curl_response->error->message) ;
        }
    }


    echo json_encode($response);