如何从控制器Laravel调用API而不使用curl和guzzle,因为它不起作用

Mah*_*tap 3 api call laravel laravel-5

如何使用laravel中的助手从控制器调用API而不使用curl和guzzle,因为两者都重试了我在邮递员api中测试过的内容,但工作正常,但在laravel中却没有。我需要从其他控制器调用serval API,所以什么才是最好的I need a helper for this

我使用curl,但是它总是给我错误的答复。

Aka*_*rma 12

您可以使用Guzzle pacakge文档:https//github.com/guzzle/guzzle

安装

composer require guzzlehttp/guzzle
Run Code Online (Sandbox Code Playgroud)

得到

    $client = new \GuzzleHttp\Client();
    $request = $client->get('example.com');
    $response = $request->getBody();
    return $response;
Run Code Online (Sandbox Code Playgroud)

开机自检

    $client = new \GuzzleHttp\Client();
    $body['name'] = "Testing";
    $url = "http://my-domain.com/api/v1/post";
    $response = $client->createRequest("POST", $url, ['body'=>$body]);
    $response = $client->send($response);
    return $response;
Run Code Online (Sandbox Code Playgroud)

一些有用的方法

    $response->getStatusCode(); # 200
    $response->getHeaderLine('content-type'); # 'application/json; charset=utf8'
    $response->getBody(); # '{"id": 1420053, "name": "guzzle", ...}'
Run Code Online (Sandbox Code Playgroud)
  • 我们也可以发送异步请求
    $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
    $promise = $client->sendAsync($request)->then(function ($response) {
        echo 'I completed! ' . $response->getBody();
    });
    $promise->wait();
Run Code Online (Sandbox Code Playgroud)
  • 我们还可以添加标题
    $header = array('Authorization'=>'token');
    $client = new \GuzzleHttp\Client();
    $request = $client->get('example.com',array('headers' => $header));
    $response = $request->getBody();
Run Code Online (Sandbox Code Playgroud)

此方法的助手

我们可以为这些方法创建一个通用帮助器。

  • 首先在应用程序文件夹中创建文件夹
    app\Helper
Run Code Online (Sandbox Code Playgroud)
  • 然后在Helper文件夹中创建一个文件
    app\Helper\helper.php
Run Code Online (Sandbox Code Playgroud)
  • 将此代码添加到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->createRequest("POST", $url, ['body'=>$body]);
        $response = $client->send($response);
        return $response;
    }
}

Run Code Online (Sandbox Code Playgroud)
  • 现在在控制器中使用
    use App\Helper\Helper;
    Helper::GetApi('ultimateakash.com');
Run Code Online (Sandbox Code Playgroud)

我们也可以在没有帮助的情况下做到这一点

  • 在主控制器中,我们可以创建这些功能
<?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->createRequest("POST", $url, ['body'=>$body]);
        $response = $client->send($response);
        return $response;
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 现在在控制器中我们可以调用
    $this->GetApi('ultimateakash.com');
Run Code Online (Sandbox Code Playgroud)

  • 不过,对我来说,这似乎只不过是关于如何使用Guzzle的教程。关于如何提供“最佳方式”在多个控制器中使用多个API的最初问题仍然悬而未决。是否应将此代码复制到每个单独的控制器? (2认同)
  • @NicoHaase我按照您的建议编辑了答案 (2认同)