【问题标题】:is that possible sending http request from lumen using guzzlehttp?是否可以使用 guzzlehttp 从 lumen 发送 http 请求?
【发布时间】:2021-01-27 15:14:24
【问题描述】:

实际上我想验证给定的令牌.. 在另一个流明包中编写的验证代码。当我发送验证令牌的请求时,我遇到了一些问题。我不知道为什么它不起作用。不能在 lumen 内使用其他 api 吗?

如果我在邮递员https://i.stack.imgur.com/kSpJt.png 中使用该检查令牌api。它工作正常。当我在其他 lumen 包中调用该 api 时抛出错误

这是我在邮递员https://i.stack.imgur.com/hfFzk.png使用这个api时得到的

错误日志https://i.stack.imgur.com/ds0oR.png

<?php

namespace App\Helpers;

use App\Helpers\ResponseBuilder;

class check_customer_token_verification 
{
    public static function check($token, $vendor_id) 
    { 
        $client = new \GuzzleHttp\Client();
            $result = $client->post('dev.adiswar-crm.local/customer/crm/v-1-0-0/check-token', [
                'form_params' => [
                    'token' => isset($token)?$token:'',
                    'vendor_id' => $vendor_id,
                ]
            ]); 

        $res_data = json_decode($result->getBody()->getContents()); dd($res_data);
            if ($res_data->http_code == 401) {
                return ResponseBuilder::responseResult(400, $res_data->message);
            }
        return $res_data;
    }
} ```

dump this api in postman. i got issue which is below


^ {#120
  +"http_code": 400
  +"message": """
    Server error: `POST dev.adiswar-crm.local/customer/crm/v-1-0-0/check-token` resulted in a `500 Internal Server Error` response:
    <!DOCTYPE html>
    <html>
        <head>
            <meta name="robots" content="noindex,nofollow" />
            <style>
            (truncated...)
    """
}```



   


 


  

【问题讨论】:

  • 嗯,我看到了 dd($res_data);在您的代码中。
  • 我只是想知道我在邮递员中得到了什么回应。这就是我在转储这个时得到的 ^ {#120 +"http_code": 400 +"message": """服务器错误:POST dev.adiswar-crm.local/customer/crm/v-1-0-0/check-token 导致 500 Internal Server Error 响应:
  • 你需要定义你的form_params

标签: php laravel lumen guzzle


【解决方案1】:

您需要定义您的 form_params,尽管您的代码可以工作,但我也建议使用 try catch 块并在 catch 块中添加您的 401 异常(以及其中的其他 400),请参阅我所做的更改

 public static function check($token, $vendor_id) 
    { 
        try{
            $client = new \GuzzleHttp\Client();
            define("form_params", \GuzzleHttp\RequestOptions::FORM_PARAMS );
            $guzzleResponse = $client->post('dev.adiswar-crm.local/customer/crm/v-1-0-0/check-token', [
                'form_params' => [
                    'token' => isset($token) && !empty($token) ? $token : '',
                    'vendor_id' => $vendor_id,
                ]
            ]); 
            if ($guzzleResponse->getStatusCode() == 200) {
               $result = json_decode($guzzleResponse->getBody(),true);
               // dd($result);
            }
            
            return $result;
        }catch(\GuzzleHttp\Exception\RequestException $e){
           // Catch all 4XX errors 
           dd($e->getMessage, $e->getTraceAsString());
           // To catch exactly error 401 use 
           if ($e->hasResponse()){
               if ($e->getResponse()->getStatusCode() == '401') {
                   return ResponseBuilder::responseResult(400, $e->getMessage());
            }
         } 
        }catch(Exception $e){
           //other errors 
        }
    }

【讨论】:

  • 谢谢你的回复兄弟.. 我用这个仍然有同样的问题.. 是否可以使用 guzzlehttp 从 lumen 发送 http 请求?
  • 是的,可以从另一个项目 api 调用一个在流明中创建的项目,通过 form_params 你基本上希望标题是application/x-www-form-urlencoded,在哪一侧你会得到 500 内部服务器错误
  • 我从 pims(lumen) 向 crm(lumen) 发送请求以验证令牌。从 pims 方面我得到了服务器错误异常
  • 您在邮递员的预览部分看到了什么,您可以编辑问题以显示邮递员的错误截图
  • 这是 $result 的 dd(),如果是,请对其进行注释,因为它是 400(甚至在 500 的情况下),它将进入 RequestException 的 catch 块,所以添加一个 @987654323 @ & 放在$e-&gt;getMessage()$e-&gt;getTraceAsString() 里面,它会在你的日志文件中显示完整的错误(对我来说是storage/logs/lumen.logs),还添加类Illuminate\Support\Facades\Log
猜你喜欢
  • 2013-05-24
  • 2015-04-14
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多