Slim PHP可以向其他服务发出请求


Can Slim PHP make request to other services?

我目前正在构建一个带有PHP后端的AngularJS应用程序。路由是使用SlimPHP完成的,我发现了一个AngularJs模块来进行基于令牌的身份验证。在后端的模块示例中,他们使用Laravel和一个名为GuzzleHttp''client()的客户端。现在,我不确定GuzzleHttp做了什么,而Slim PHP没有做什么(如果有的话),但我试图遵循他们的例子,但我不想安装两个基本上可以做同样事情的框架。

所以我已经完成了路由,这样当向后端(auth/google)发出请求时,它会这样做:

public function google()
{
    $app = 'Slim'Slim::getInstance();
    $request = $app->request()->getBody();
    $body = json_decode($request);
    $accessTokenUrl = 'https://accounts.google.com/o/oauth2/token';
    $peopleApiUrl = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect';
    $params = array(
        'code' => $body->code,
        'client_id' => $body->clientId,
        'redirect_uri' => $body->redirectUri,
        'grant_type' => 'authorization_code',
        'client_secret' => GOOGLE_SECRET
    );
    $client = new GuzzleHttp'Client();
    // Step 1. Exchange authorization code for access token.
    $accessTokenResponse = $client->post($accessTokenUrl, ['body' => $params]);
    $accessToken = $accessTokenResponse->json()['access_token'];

    $headers = array('Authorization' => 'Bearer ' . $accessToken);
    // Step 2. Retrieve profile information about the current user.
    $profileResponse = $client->get($peopleApiUrl, ['headers' => $headers]);
    $profile = $profileResponse->json();
    // Step 3a. If user is already signed in then link accounts.
    if (Request::header('Authorization'))
    {
        $user = User::where('google', '=', $profile['sub']);
        if ($user->first())
        {
            return Response::json(array('message' => 'There is already a Google account that belongs to you'), 409);
        }
        $token = explode(' ', Request::header('Authorization'))[1];
        $payloadObject = JWT::decode($token, Config::get('secrets.TOKEN_SECRET'));
        $payload = json_decode(json_encode($payloadObject), true);
        $user = User::find($payload['sub']);
        $user->google = $profile['sub'];
        $user->displayName = $user->displayName || $profile['name'];
        $user->save();
        return Response::json(array('token' => $this->createToken($user)));
    }
    // Step 3b. Create a new user account or return an existing one.
    else
    {
        $user = User::where('google', '=', $profile['sub']);
        if ($user->first())
        {
            return Response::json(array('token' => $this->createToken($user->first())));
        }
        $user = new User;
        $user->google = $profile['sub'];
        $user->displayName = $profile['name'];
        $user->save();
        return Response::json(array('token' => $this->createToken($user)));
    }
}

现在这不起作用,因为我没有安装GuzzleHttp,但我的问题是:我可以在Slim PHP中做到这一点吗?还是需要GuzzleHttp来补充它

Guzzle是一个基于代码的HTTP客户端包/框架,它还包含DOM爬网功能,而不是微框架,因此它与Slim不相似。

来自他们的自述:

Guzzle是一个PHP HTTP客户端,它使发送HTTP请求变得很容易,与web服务集成也很简单

Slim不直接提供此功能,因为它不属于Slim的职责范围,即将HTTP请求转换为HTTP响应(以及其间需要发生的核心事情)。

既然你的例子在Guzzle中,它实现了你想要做的事情,我可能会使用Guzzle。但是,您可以使用cURLext/http或其他HTTP客户端包来执行相同类型的操作(即与外部web服务交互)。有几个。

Slim是一个微框架,主要提供来自应用程序的客户端请求和响应。这意味着Slim会响应请求,例如,它不会向外部HTTP发出请求。Slim的理念是提供路线,在事情发生时做一些事情,并对客户做出回应。如果你需要使用外部调用,你需要使用任何HTTP客户端,这将提供发出请求和威胁响应的"能力"。您可以本机使用curl(所有其他的都只是curl的"接口")或lib。

虽然Slim和Guzzle有很多共同点,即它们都处理psr-7请求和响应

有一个关键区别Slim处理请求和发送响应Guzzle处理发送请求和处理响应

因此,它们是不可互换的,并且处理通信管道的两端

因此,如果有人向你发送了处理请求,你需要苗条或类似的东西如果你正在向其他人发送请求,那么你需要guzzle或类似的