How to login with Kakao API (PHP)
I found myself the other day fighting with the Kakao API to implement an easy login function on my web app (PHP). So I thought I'd share here my experience and show you how to quickly achieve this. By the way, I hope that one day all the Koreans API providers will be smart enough to translater their documentation in english...
Kakao for developers (app creation and settings)Let's start by setting up your app on the Kakao for developers platform. 1. Go to https://developers.kakao.com/ and login or create an account. 2. Click on the create new App button (앱만들기). 3. Choose a name for your app. 4. On the left menu click on general (일반) and then click on add a platform (플랫폼 추가). 5. Choose the platform you want to work with (in this example we use the web app), so select the option on the right (웹) and enter your website address. Confirm by clicking on the dark grey button (추가). 6. Once the platform has been added you can see you website url and also you can modify the redirect back url. Let's change the default one to something that makes more sense like: kakao_login. Don't forget to confirm by clicking on 전장. 7. Next we have to go utilities admin and "turn on" our app. Toggle the on / off button by clicking on it. Once the app is activated you'll see a bunch of new options to set. I have to admit that I don't know everything ^^ but I can tell you that you don't need to touch the first part and on the bottom part where there are radio boxes and inputs to fill you can just fill the inputs with anything. Those inputs are the description of why you need to access those services but I haven't seen where exactly does it impact your app yet. By default all the services (kakao talk / kakao story / kakao login etc..) are activated (left radio box checked) but if you don't need everything you can check the radio box on the right that says: don't use. So in my case I only need to access to the profile of the user which is the first input only. And I just wrote for the purpose: "those information are used to login users". And checked the right radio box for the rest. When you finish don't forget to confirm with the button at the bottom (전장).
The code (PHP)First create a login button in your HTML code. The URL of your button should be something like this:
The redirect uri is your http://yourdomain/the-callback-we-set-earlier. Your client ID is the REST API key that you can find in the summary page of your app. Left menu (개요) then click on 웹키 표시 to display the keys. Finally create a kakao_login.php page, get the authorize code from the Kakao server, request for an access token and write the code to save in your DB and/or authenticate your users. Here is a code example to modify according to your needs: (Don't forget to change your app key and redirect url with your information)
Hope I saved someone :) Don't hesitate to ask questions in the comments.
// Get the code from kakao server $authorize_code = $_GET['code']; // POST the code to get an access token $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://kauth.kakao.com/oauth/token"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&client_id=YOURAPPKEY&redirect_uri=http://YOURDOMAIN/kakao_loginh&code=$authorize_code"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch); $server_output = json_decode($server_output, true); $access_token = $server_output['access_token']; // POST the access token to get user info $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://kapi.kakao.com/v1/user/me"); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer '.$access_token )); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); $server_output = json_decode($server_output, true); curl_close ($ch); $kakao_id = $server_output['id']; $kakao_nickname = $server_output['properties']['nickname']; $kakao_profile_image = $server_output['properties']['profile_image']; $kakao_thumbnail_image = $server_output['properties']['thumbnail_image'];