【问题标题】:Guzzle vs CURL for interacting with vtiger web services, CURL works but Guzzle does notGuzzle vs CURL 用于与 vtiger Web 服务交互,CURL 有效,但 Guzzle 无效
【发布时间】:2017-08-30 22:22:01
【问题描述】:

我正在实现一个客户端来使用 vtiger REST API,在登录过程中,我设法让它与 curl 一起工作,但不能与 Guzzle 一起工作。

Guzzle 代码:

$postData = [
    'operation' => 'login',
    'username' => $userName,
    'accessKey' => $generatedKey
];

$response = $client->post($url, [
    'form_params' => $postData
]);

没有实际的 Guzzle 错误或异常,只是我无法进行身份验证:

{"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 中不起作用,但它使用 curl。有什么想法吗?

【问题讨论】:

  • 请提供有关由此产生的错误的更多信息。使用服务器的 Guzzle 版本时出现什么错误?
  • @AlexeyShockov 没有实际的 Guzzle 错误,但尽管发送了正确的数据,但我没有得到预期的响应

标签: php curl guzzle vtiger


【解决方案1】:
 $response = $client->request('POST','',['form_params' => ['operation'=>'getchallenge', 'username'=>$userName]  ] ) 

以上行不通,但返回 crm_token 和

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

工作正常。

【讨论】:

    【解决方案2】:

    第一个代码中的$generatedKey 是否与第二个代码中的md5($crm_token.$crm_useraccesskey) 相同? 如果否,则更正它,它可能有效。如果没有,根据 Guzzle 6.0 的文档,对于 post requests ,您可以执行以下操作:

    $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

    【讨论】:

      【解决方案3】:

      聚会真的迟到了,但希望这能帮助遇到同样问题的其他人。

      这对使用 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 执行其他查询

      【讨论】:

        猜你喜欢
        • 2020-10-06
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        相关资源
        最近更新 更多