dongxianghui3709 2015-02-11 18:09
浏览 57

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 = '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)));
    }
}

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?

  • 写回答

3条回答 默认 最新

  • dqzow3859 2015-02-11 18:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办