向Laravel 5.2添加外部API

时间:2016-04-03 14:54:28

标签: php api laravel laravel-5.2

我是Laravel的新东西和API的新手(以前从未这样做过),但我知道PHP和Javascript。但我想尽快学习这些东西,很抱歉,如果我的问题很愚蠢:D

所以我想问你们的是:

  1. 是否可以在Laravel 5.2中添加外部API?我需要把它放在哪里?
  2.   

    因为有人说您无法向Laravel添加外部API。

    1. 如何将此API应用于Laravel(如果可能的话)?
    2. POST /api/oauth/token HTTP/1.1
      Host: api.######.####
      Authorization: Basic /* some unique strings and numbers here */
      Content-Type: application/x-www-form-urlencoded
      
      grant_type=client_credentials
      

      对不起我的英语,但我已经尽力解释我想要的内容。 谢谢:))

2 个答案:

答案 0 :(得分:2)

不知道这些“有些人”,但你可以用Laravel做任何你想做的事情,它没有任何限制。只要PHP可以做到,Laravel就可以做到。

搜索实现API的包,例如

composer require <api-package>

例如要在PHP中使用Instagram API,可以试试这个包:

composer require cosenary/instagram

所有依赖项都在composer.json中监听。

如果找不到,使用php-curl可以轻松实现HTTP API包。 (curl中命令行中的PHP)。阅读相应的API文档,并逐一实现API的每个资源路由。

可以找到curl的完整PHP文档here

答案 1 :(得分:2)

使用guzzle,并对以下示例的任何网络服务执行您的请求:

$response = $client->request('POST', 'http://example.com/api/oauth/token', [
        'headers' = [
            'Authorization' => 'Basic your_token'
        ],
        'form_params' => [
            'grant_type' => 'client_credentials',
            ]
        ]
    ]);

请参阅此处http://guzzle.readthedocs.org/en/latest/overview.html