PHP 페이스북 한국어 이름 얻기

How to get language-specific name in Facebook API, FQL
페북 SDK 모국어 이름 읽어오기
페이스북 SDK 한국어 이름 얻기
페이스북 PHP 다국어 이름, 언어 특정 이름

1 모국어 이름[ | ]

페이스북은 가입시에 영어이름을 필요로 한다. 가입 이후에 환경설정에서 자신의 언어를 설정하면 그 언어에 맞는 이름을 추가로 등록할 수 있다. 그것이 모국어 이름(language-specific name)[1]이다.

2 핵심 코드[ | ]

다음과 같이 FQL을 실행하되 locale을 ko_KR로 주면 된다.

$result = fql_query("SELECT name FROM user WHERE uid=me()", 'ko_KR');

3 전체 코드[ | ]

<?php
function xmp_print_r($arr) { echo '<xmp>'; print_r($arr); echo '</xmp>'; }
function fql_query($querys, $locale='en_US') {
        global $app_id;
        global $app_secret;
        $my_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
        if(!isset($_REQUEST["code"])) {
                $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id='.$app_id.'&redirect_uri='.urlencode($my_url);
                echo("<script>top.location.href='".$dialog_url."'</script>");
                return;
        }
        if(is_array($querys)) {
                for($i=0;$i<count($querys);$i++) {
                        $query = urlencode($querys[$i]);
                        $querys[$i] = '"'.$i.'":"'.$query.'"';
                }
                $q_str = '{'.implode(',', $querys).'}';
        } else $q_str = urlencode($querys);
 
        $token_url = "https://graph.facebook.com/oauth/access_token?client_id=$app_id"
  .'&redirect_uri='.urlencode($my_url)."&client_secret=$app_secret&code=".$_REQUEST['code'];
        $access_token = substr(file_get_contents($token_url), 13);
        $fql_query_url = "https://graph.facebook.com//fql?q=$q_str&access_token=$access_token&locale=$locale";
        $fql_query_result = file_get_contents($fql_query_url);
        return json_decode($fql_query_result, true);
}

$app_id = '앱아이디';
$app_secret = '앱비밀번호';
$result = fql_query("SELECT name FROM user WHERE uid=me()", 'ko_KR');
xmp_print_r($result);
?>
→ 자신의 앱아이디와 앱비밀번호를 입력.[2]

3.1 결과[ | ]

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [name] => 홍길동
                )

        )
)

4 같이 보기[ | ]

5 주석[ | ]

  1. 직역하면 '언어-특정 이름'이지만, 이해하기 쉽도록 필자는 '모국어 이름'으로 지칭한다.
  2. 앱아이디가 없으면 페이스북 앱ID 발급 참조

6 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}