2016-05-30 67 views

回答

5

User Profile API可能會幫助你。

使用來自信使BOT服務器(/網絡掛接)收到event.sender.id,並按照以下

curl -X GET "https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=<PAGE_ACCESS_TOKEN>" 

請求,那麼你可以得到以下

{ 
    "first_name": "Peter", 
    "last_name": "Chang", 
    "profile_pic": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/13055603_10105219398495383_8237637584159975445_n.jpg?oh=1d241d4b6d4dac50eaf9bb73288ea192&oe=57AF5C03&__gda__=1470213755_ab17c8c8e3a0a447fed3f272fa2179ce", 
    "locale": "en_US", 
    "timezone": -7, 
    "gender": "male" 
} 
+0

所以我只是把curl的東西粘貼到php代碼中吧? 我如何訪問返回的json? (即我想說的「你好」+ <人的名字>) –

+0

看到如何使一個HTTP獲取請求和訪問返回的JSON在PHP http://stackoverflow.com/questions/15617512/get-json- object-from-url – iownthegame

+0

我應該激活哪些權限? – enriquo

0

您可以使用下面的返回的JSON PHP片段獲取用戶名稱

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name&access_token=<PAGE_ACCESS_TOKEN>'); 
$result = curl_exec($ch); 
curl_close($ch); 

$obj = json_decode($result); 
echo 'Hi ' . $obj['first_name'] . ' ' . $obj['last_name'] 
+0

對不起,我從來沒有真正的使用PHP。我該如何使用你的PHP代碼片段(即,我把它放在代碼中)? 我有我在下面的評論中基於你的代碼。 –

+0

if($ messageText ==「hi」){ // $ answer =「Hello」; $ ch = curl_init(); curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ ch,CURLOPT_URL,'https://graph.facebook.com/v2.6/ ?fields = first_name,last_name&access_token = '); $ result = curl_exec($ ch); curl_close($ ch); $ obj = json_decode($ result); $ answer ='嗨'。 $ obj ['first_name']。 ''。 $ OBJ [ '姓氏']; } else if($ messageText!=「」){ $ answer =「對不起,我不認爲我理解你的意思,試着問我別的東西! } –

+0

@RayyaanMustafa,你必須粘貼這個代碼,你正在處理的消息文本。 –

0

@Rajesh Hedge

您的代碼有一個小錯誤:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name&access_token=<PAGE_ACCESS_TOKEN>'); 
$result = curl_exec($ch); 
curl_close($ch); 

$obj = json_decode($result); // *** here 
echo 'Hi ' . $obj['first_name'] . ' ' . $obj['last_name'] 

$obj = json_decode($result, **true**); 

$result需要被轉化爲關聯數組,然後才能像這樣訪問:$obj['first_name']

詳見http://php.net/manual/en/function.json-decode.php

相關問題