2016-08-24 27 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進行其他查詢