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 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键