2016-08-24 98 views
1

我正在实现一个客户端来使用vtiger REST API,并在登录过程中设法让它使用curl而不是使用Guzzle。Guzzle vs CURL与vtiger网络服务交互,CURL工程,但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"}} 

卷曲版本:

$curl = curl_init($service_url); 
$curl_post_data = array(
    'operation' => 'login', 
    'username' => $crm_username, 
    'accessKey' => md5($crm_token.$crm_useraccesskey), 
); 

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); 

我更喜欢使用Guzzle,但现在我不知道为什么它不能在Guzzle中工作,但它确实使用卷曲。有任何想法吗?

+0

请提供有关导致错误的详细信息工作正常,我。 Guzzle版本从服务器上得到什么错误? –

+0

@AlexeyShockov没有实际的Guzzle错误,但我没有得到预期的响应,尽管发送了正确的数据 – DanielRestrepo

回答

0

$generatedKey中的第一个代码与第二个代码中的md5($crm_token.$crm_useraccesskey)相同吗? 如果否,然后更正它,它可能工作。如果不是这样,基于对狂饮6.0文档,POST请求,你可以做如下:

$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

1
$response = $client->request('POST','',['form_params' => ['operation'=>'getchallenge', 'username'=>$userName] ]) 

以上都不行,但返回crm_token和

$response = $client->request('GET','',['query' => ['operation'=>'getchallenge', 'username'=>$userName] ])

工作正常。

0

真的很晚参加派对,但希望这会帮助有同样问题的其他人。

此使用狂饮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; 

然后你可以使用$会话ID进行其他查询