我正在实现一个使用vtiger REST API的客户端,并且在登录过程中我设法使用curl而不是Guzzle。
Guzzle代码:
<Dial>
没有实际的Guzzle错误或异常,但只是我无法进行身份验证:
$postData = [
'operation' => 'login',
'username' => $userName,
'accessKey' => $generatedKey
];
$response = $client->post($url, [
'form_params' => $postData
]);
卷曲版本:
{"success":false,"error":{"code":"INVALID_AUTH_TOKEN","message":"Specified token is invalid or expired"}}
我更喜欢使用Guzzle但是现在我不知道为什么它在Guzzle中不起作用但它确实使用了curl。有什么想法吗?
答案 0 :(得分:1)
$response = $client->request('POST','',['form_params' => ['operation'=>'getchallenge', 'username'=>$userName] ] )
以上不会奏效,但会返回crm_token和
$response = $client->request('GET','',['query' => ['operation'=>'getchallenge', 'username'=>$userName] ] )
工作正常。
答案 1 :(得分:0)
第一个代码中的$generatedKey
与第二个代码中的md5($crm_token.$crm_useraccesskey)
是否相同?
如果不是,那么纠正它,它可能会起作用。如果没有,根据Guzzle 6.0的文档,对于发布请求,您可以执行以下操作:
$postData = [
'operation' => 'login',
'username' => $userName,
'accessKey' => $generatedKey
];
$response = $client->request('POST',$url, [
'form_params' => $postData
]);
了解更多信息,请参阅: http://docs.guzzlephp.org/en/latest/request-options.html#form-params
答案 2 :(得分:0)
派对真的很晚,但希望这会帮助那些遇到同样问题的人。
使用Guzzle 6.0这对我来说很好用
use GuzzleHttp\Client;
// vTiger API constants
define('VT_URL', 'http://yoursite.com/webservice.php');
define('VT_USERNAME', 'the_name_of_the_user');
define('VT_ACCESSKEY', 'your_accesskey');
$client = new Client(); //GuzzleHttp\Client
// perform API GET request
$reponse = $client->request('GET', VT_URL, [
'query' => [
'operation' => 'getchallenge',
'username' => VT_USERNAME
]
]);
// decode the response
$challenge = json_decode($reponse->getBody());
// If challenge failed
if($reponse->getStatusCode() !== 200 || !$challenge->success) {
die('getchallenge failed: ' . $challenge['error']['errorMessage']);
}
// Everything ok so create a token from response
$token = $challenge->result->token;
// Create unique key using combination of challengetoken and accesskey
$generatedkey = md5($token . VT_ACCESSKEY);
// login using username and accesskey
$reponse = $client->request('POST', VT_URL, [
'form_params' => [
'operation' => 'login',
'username' => VT_USERNAME,
'accessKey' => $generatedkey
]
]);
// decode the response
$login_result = json_decode($reponse->getBody()->getContents());
// If api login failed
if($reponse->getStatusCode() !== 200 || !$login_result->success) {
die('login failed: ' . $login_success['error']['errorMsg']);
}
$sessionid = $login_result->result->sessionName;
然后您可以使用$ sessionid执行其他查询