使用Guzzle http请求在Guzzle 7中获取API响应URL参数

发布于 2025-01-26 06:55:39 字数 1006 浏览 2 评论 0 原文

我在网上搜索,但找不到适当的解决方案。我正在使用Guzzle Client拨打春季服务,以防其URL参数提供错误消息,例如: http:// localhost:8085/fedauth/null?errmessage = mot% 20DE%20PASSE%20Invalide%20POUR%20L& apos; litilisateur%20Karan%20sharma。。如何使用Guzzle来获取此param errmessage 。以下是我的php中的Slim代码。

  $data = [
    'userName' => base64_encode($userName),
    'userPassword' => base64_encode($userPassword),
    'institution' => $institution,
    'redirectUrl' => $redirectUrl,
    'callerUrl' => $callerUrl,
    'clientId' => $clientId,
    'encryptMode' => $encryptMode,
    'moodleLandPage' => $moodleLandPage,
    'login' => $login,
    'isEncrypted' => true
];


try {

     $apiResponse = $client->post( $_ENV['FEDAUTH_API_URL'], ['form_params'=> $data]);
   } catch (Exception $exception) {

        return $response->write(json_encode(['error' => $exception->getMessage(),  "auth" => "0" ]));
    }

我尝试使用geteffectiveurl()方法,但在guzzle 7中不再支持它

I searched online but couldn't find a proper solution. I am calling a Spring service with a POST request using Guzzle Client, The service in case of any errors provides the error message in its URL param like: http://localhost:8085/fedauth/null?errMessage=Mot%20de%20passe%20invalide%20pour%20l'utilisateur%20Karan%20Sharma.. How can I fetch this param errMessage using Guzzle. Below is my code with Slim in PHP.

  $data = [
    'userName' => base64_encode($userName),
    'userPassword' => base64_encode($userPassword),
    'institution' => $institution,
    'redirectUrl' => $redirectUrl,
    'callerUrl' => $callerUrl,
    'clientId' => $clientId,
    'encryptMode' => $encryptMode,
    'moodleLandPage' => $moodleLandPage,
    'login' => $login,
    'isEncrypted' => true
];


try {

     $apiResponse = $client->post( $_ENV['FEDAUTH_API_URL'], ['form_params'=> $data]);
   } catch (Exception $exception) {

        return $response->write(json_encode(['error' => $exception->getMessage(),  "auth" => "0" ]));
    }

I have tried using the getEffectiveUrl() method but its no longer supported in Guzzle 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2025-02-02 06:55:39

我想您将响应作为重定向URL?在这一点上,您的问题尚不清楚。在这种情况下,您可以这样访问它:

$apiResponse = $client->post( $_ENV['FEDAUTH_API_URL'], ['form_params'=> $data]);
echo $apiResponse->getEffectiveUrl();

就像:

I guess you get the response as a redirect url? Your question is not clear in that point. In this case you can access it like this:

$apiResponse = $client->post( $_ENV['FEDAUTH_API_URL'], ['form_params'=> $data]);
echo $apiResponse->getEffectiveUrl();

like here: https://docs.guzzlephp.org/en/5.3/http-messages.html#responses

隔岸观火 2025-02-02 06:55:39

实际找到了答案。您需要添加Track Redirects选项为true,然后使用 $ response-> getheaderline('x-guzzle-redirect-History'); 如下

$client = new GuzzleHttp\Client(['headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded'], 'verify' => false, 'allow_redirects' => ['track_redirects' => true]]);
$apiResponse = $client->post( $_ENV['FEDAUTH_API_URL'], ['form_params'=> $data]);
echo $apiResponse->getHeaderLine('X-Guzzle-Redirect-History');

Actually found the answer. You need to add track redirects option as true and then use $response->getHeaderLine('X-Guzzle-Redirect-History'); like below

$client = new GuzzleHttp\Client(['headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded'], 'verify' => false, 'allow_redirects' => ['track_redirects' => true]]);
$apiResponse = $client->post( $_ENV['FEDAUTH_API_URL'], ['form_params'=> $data]);
echo $apiResponse->getHeaderLine('X-Guzzle-Redirect-History');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文