laravel - 如何在不使用 curl 和 guzzle 的情况下从 Controller Laravel 调用 API,因为它不起作用

标签 laravel api laravel-5 guzzle

关闭。这个问题需要details or clarity .它目前不接受答案。












想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题.

2年前关闭。




Improve this question




如何使用 Laravel 中的帮助程序从 Controller 调用 API,而不使用 curl 和 guzzle,因为两者都没有返回任何内容。我已经在 postman 中测试过,api 工作正常,但在 laravel 中却没有。
我需要从不同的 Controller 调用多个 API,那么有什么好的方法可以做到这一点?我应该建立一个助手吗?

我使用 curl 但它总是给我错误的响应。

编辑:

我正在寻找一种可靠的方法来对各种 url 进行 api 调用,而不必为我想要使用的每个 api 重写发送和接收代码。
最好是实现“干”(不要重复自己)原则的解决方案,并将作为任何 api 特定逻辑的基础,(解析响应/翻译模型)。那东西会扩展这个基础。

最佳答案

你可以使用 Guzzle pacakge
文档:https://github.com/guzzle/guzzle
安装

composer require guzzlehttp/guzzle
得到
    $client = new \GuzzleHttp\Client();
    $request = $client->get('example.com');
    $response = $request->getBody();
    return $response;
邮政
    $client = new \GuzzleHttp\Client();
    $body['name'] = "Testing";
    $url = "http://my-domain.com/api/v1/post";
    $response = $client->request("POST", $url, ['form_params'=>$body]);
    $response = $client->send($response);
    return $response;
一些有用的方法
    $response->getStatusCode(); # 200
    $response->getHeaderLine('content-type'); # 'application/json; charset=utf8'
    $response->getBody(); # '{"id": 1420053, "name": "guzzle", ...}'
  • 我们也可以发送异步请求

  •     $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
        $promise = $client->sendAsync($request)->then(function ($response) {
            echo 'I completed! ' . $response->getBody();
        });
        $promise->wait();
    
  • 我们还可以添加标题

  •     $header = array('Authorization'=>'token');
        $client = new \GuzzleHttp\Client();
        $request = $client->get('example.com',array('headers' => $header));
        $response = $request->getBody();
    
    此方法的助手
    我们可以为这些方法创建一个通用的助手。
  • 首先在app文件夹中创建文件夹

  •     app\Helper
    
  • 然后在 Helper 文件夹中创建一个文件

  •     app\Helper\Helper.php
    
  • 将此代码添加到 Helper.php

  • <?php 
    
    namespace App\Helper;
    class Helper
    {
    
        public static function GetApi($url)
        {
            $client = new \GuzzleHttp\Client();
            $request = $client->get($url);
            $response = $request->getBody();
            return $response;
        }
    
    
        public static function PostApi($url,$body) {
            $client = new \GuzzleHttp\Client();
            $response = $client->request("POST", $url, ['form_params'=>$body]);
            $response = $client->send($response);
            return $response;
        }
    }
    
    
  • 现在在 Controller 中使用

  •     use App\Helper\Helper;
        Helper::GetApi('ultimateakash.com');
    
    我们也可以在没有 helper 的情况下做到这一点
  • 在主 Controller 中,我们可以创建这些函数

  • <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Foundation\Bus\DispatchesJobs;
    use Illuminate\Routing\Controller as BaseController;
    use Illuminate\Foundation\Validation\ValidatesRequests;
    use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
    
    class Controller extends BaseController
    {
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    
        public function GetApi($url)
        {
            $client = new \GuzzleHttp\Client();
            $request = $client->get($url);
            $response = $request->getBody();
            return $response;
        }
    
    
        public function PostApi($url,$body) {
            $client = new \GuzzleHttp\Client();
            $response = $client->request("POST", $url, ['form_params'=>$body]);
            $response = $client->send($response);
            return $response;
        }
    }
    
  • 现在在 Controller 中我们可以调用

  •     $this->GetApi('ultimateakash.com');
    

    关于laravel - 如何在不使用 curl 和 guzzle 的情况下从 Controller Laravel 调用 API,因为它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56071154/

    相关文章:

    laravel - 在中间件上路由错误

    php - 在 Laravel 中对只读模型进行单元测试的正确方法是什么?

    python - 使用 Tenor 的 API

    json - 带 API 的 Ruby 传递 header

    php - 使用/导入 Laravel facades 的正确方法是什么?

    javascript - 收集多个数据并在 laravel 中通过 ajax 发送

    api - 测试调用相同私有(private)方法的多个公共(public)方法

    php - 想修改 laravel 默认登录以检查用户是否已验证

    php - laravel 5.5 HTTP ERROR 500 无法处理此请求

    python - 为什么 Laravel Redis::scan ('*' ) 返回预期的 key ,但 Redis::keys ('*' ) 没有返回?