Slim PHP可以向其他服务提出请求吗?

I'm currently building an AngularJS application with a PHP backend. The routing is done using Slim PHP and I've found an AngularJs module to do token-based authentication. In the module example for the backend they use Laravel and a client called GuzzleHttp\Client(). Now, I'm not sure what GuzzleHttp do that Slim PHP don't (if any) but I'm trying to follow along their example but I don't want to install 2 frameworks that could essentially do the same thing.

So I have my routing done so that when a request is made to the backend (auth/google) it'll do this:

public function google()
    $app = \Slim\Slim::getInstance();
    $request = $app->request()->getBody();
    $body = json_decode($request);

    $accessTokenUrl = '';
    $peopleApiUrl = '';
    $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'];
        return Response::json(array('token' => $this->createToken($user)));
    // Step 3b. Create a new user account or return an existing one.
        $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'];
        return Response::json(array('token' => $this->createToken($user)));

Now this won't work because I don't have GuzzleHttp installed but my question is: can I do this in Slim PHP or do I need GuzzleHttp to complement it?



Guzzle是一个基于代码的HTTP客户端包/框架,它还包含DOM爬行功能,而不是微框架,因此它不类似于 瘦。 </ p>

从他们的自述文件:</ p>

Guzzle是一个PHP HTTP客户端,可以轻松发送HTTP请求和琐碎 与Web服务集成。</ strong> </ p>
</ blockquote>

Slim不直接提供此功能,因为它不属于Slim的意图,即转换HTTP请求 进入HTTP响应(以及需要在其间发生的核心事情)。</ p>

由于你的例子是在Guzzle中并且它实现了你想要做的事情,我可能会使用Guzzle。 但是,您可以使用 cURL </ code>, ext / http </ code>或其他HTTP客户端软件包执行相同类型的事务(即与外部Web服务交互)。 有几个。</ p>
</ div>



Guzzle is a code based HTTP client package/framework which also contains DOM crawling functionality not a micro-framework, thus it is not analogous to Slim.

From their Readme:

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.

Slim does not provide this functionality directly because it doesnt fall under what Slim is meant to do which is transform HTTP requests into HTTP responses (and the core things that need to happen in between).

Since your example is in Guzzle and it implements what you are trying to do i would probably use Guzzle. However, you could do the same types of thing (ie. interact with an external web service) using cURL, ext/http, or another HTTP client package. There are several.

Slim是一个微框架,它基本上提供来自应用程序的客户端请求和响应。 这意味着Slim会响应请求,例如,它不会向外部HTTP发出请求</ strong>。 Slim的想法是提供路由,在它到来时做一些事情并响应客户端。
如果你需要使用外部调用,你需要使用任何HTTP客户端,它将提供“能力”来发出请求 和响应的威胁。 你可以原生使用curl(所有其他的只是卷曲的“接口”)或lib。 </ p>
</ div>



Slim is a micro-framework that provides basically client requests and responses, coming from the application. It means that Slim responds to request, it doesn't make requests to external HTTP, for instance. The idea of Slim is to provide routes, do stuff when it comes and responds to the client. If you need to use external calls, you'll need to use any HTTP Client, that'll provide the "ability" to make requests and threat with the response. You can use curl natively (all the others are only "interfaces" to curl) or a lib.

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

Guzzle处理发送请求和处理响应</ p>

因此它们不可互换并处理相反的两端 通信管道</ p>


如果您向其他人发送请求,那么您需要guzzle或类似的东西</ p>

</ div>



While Slim and Guzzle have a lot in common ie they both deal with psr-7 requests and responses

There is one key difference Slim Deals with processing requests and sending responses Guzzle Deals with sending requests and processing responses

as such they are not interchangeable and deal with opposite ends of the communication pipeline

so if your processing requests that someone has sent to you you need slim or something similar if you are sending requests to someone else then you need guzzle or something similar

Csdn user default icon