【问题标题】:Calling external API from Laravel Controller从 Laravel 控制器调用外部 API
【发布时间】:2021-08-02 04:06:59
【问题描述】:

我正在尝试调用某个服务提供商的 API,它有 public and secret API keys

不过,我目前正在使用 Laravel,但是关于如何发送 post 请求,只有 JavaScriptNodeJSPython 实现。

我的问题是,如何在 Laravel/PHP 上发送发布请求以避免公开 API 密钥?

它们具有应遵循的特定格式:

const options = {


method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Basic some_base64_encrypted_key'
  },
  body: JSON.stringify({
    data: {
      attributes: {
        amount: 10000,
        redirect: {success: 'https://www.test1.com/', failed: 'https://www.test2.com/'},
        type: 'some_paymenth_method',
        currency: 'SOME_CURRENCY'
      }
    }
  })
};

fetch('https://api.serviceprovider.com/v1/sources', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

【问题讨论】:

  • 阅读Laravel's HTTP Client。我没有处理基本授权的请求,但其中有一些通用信息可能会有所帮助

标签: php laravel api http-post


【解决方案1】:

这样的事情应该可以帮助你:

$response = Http::withBasicAuth('keys', 'secret')
        ->withHeaders([
            'Content-Type' => 'application/json',
            'Authorization' => 'Basic some_base64_encrypted_key'
        ])
        ->post('https://api.serviceprovider.com/v1/sources', [
            'amount' => '1000',
            'type' => 'some_paymenth_method',
            'currency' => 'SOME_CURRENCY'
        ]);

        if( $response->successful() ){
            
            //do some logic
            //redirect https://www.test1.com/
    
        }elseif( $response->failed() ){

            //do some logic
            //redirect https://www.test2.com/

        }

你可以玩弄它,试试文档。

https://laravel.com/docs/8.x/http-client#authentication

【讨论】:

    【解决方案2】:

    使用 HTTPS,您的 authorization 标头也被加密。因此,如果有人截获消息,他们就无法读取令牌的实际内容。但是,客户端和服务器仍然可以看到标头。在您的情况下,您是服务提供商的客户端。

    话虽如此,正如之前的anwser解释得很好,您可以使用Laravel HTTP client。这个解决方案在 laravel 7+ 上可用。你需要像这样安装Guzzle 包:

    composer require guzzlehttp/guzzle
    

    并使用Http 外观发出这样的请求:

    use Illuminate\Support\Facades\Http;
    
    $response = Http::withToken('place api key')->withHeaders([
        'Content-Type' => 'application/json',
    ])->post('https://api.serviceprovider.com/v1/sources', [
         // your data array
    ]);
    
    // Determine if the status code is >= 200 and < 300...
    if ($response->successful()) {
        // todo i.e get the response body save the date etc.
    } else {
        // todo i.e schedule to try again later etc.
    }
    

    如果你运行的是旧版本的 laravel,在使用 composer 安装 guzzlehttp/guzzle 包后,你可以这样提出请求:

    $headers = [
        'Authorization' => 'place api key',
        'Accept'        => 'application/json',
    ];
    
    $body = [
        // your data array
    ];
    
    $client = new \GuzzleHttp\Client();
    $url = "https://api.serviceprovider.com/v1/sources";
    $response = $client->request('POST', $url,  [
        'headers'=> $headers,
        'body'=> $body
    ]);
    
    // check response here
    // don't forget error handling
    
    

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2014-09-08
      • 2013-11-28
      • 1970-01-01
      相关资源
      最近更新 更多