【问题标题】:Laravel - How to include API Subscription key in my Laravel End PointLaravel - 如何在我的 Laravel 端点中包含 API 订阅密钥
【发布时间】:2020-09-06 04:14:59
【问题描述】:

在我的 Laravel-5.8 中,我尝试使用 Guzzle 使用 API 端点 GET 请求:

  use App\Employee;
  use App\Department;

  public function index() 
  {  
      $client = new GuzzleHttp\Client();
       $res = $client->request('GET','https://api.employees.net/allemployees')->getBody();

        $clientdatas = json_decode($res, true);

     ...
  }

API 有订阅密钥。

在 POSTMAN 中,我是这样做的,而且效果很好:

GET        https://api.employees.net/allemployees?key=fgfdhsddddDDDDD

如何将订阅密钥添加到我的 Laravel api

  public function index() 
  {  
      $client = new GuzzleHttp\Client();
       $res = $client->request('GET','https://api.employees.net/allemployees')->getBody();

        $clientdatas = json_decode($res, true);

     ...
  }

谢谢。

【问题讨论】:

  • 您不应该从 Postman 和从 postman 调用的方法中执行相同的 URL 请求。再次检查逻辑应该是什么任务。另外,尝试request()->get('key') 从 Postman 请求中获取价值。

标签: laravel api


【解决方案1】:

我想你要问的是如何send query string parameters in Guzzle。您可以使用以下命令将您的 API 密钥添加到您的请求中:

public function index() 
  {  
      $client = new GuzzleHttp\Client();
       $res = $client->request('GET','https://api.employees.net/allemployees?key=fgfdhsddddDDDDD')->getBody();

        $clientdatas = json_decode($res, true);

     ...
  }

public function index() 
  {  
      $client = new GuzzleHttp\Client();
       $res = $client->request('GET','https://api.employees.net/allemployees', [
           'query' => ['key' => 'fgfdhsddddDDDDD']
       ])->getBody();

        $clientdatas = json_decode($res, true);

     ...
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-03
    • 2019-12-06
    • 2018-07-01
    • 2017-04-30
    • 2018-03-14
    • 2017-12-21
    • 2018-09-20
    • 1970-01-01
    相关资源
    最近更新 更多