我在使用新的Facebook API時遇到問題。我有一個簡單的測試頁,看起來像這樣,基本上直接從文檔示例中複製:用於PHP POST問題的facebook API
<?php
// require PHP SDK
require 'facebook.php';
// Create our application instance
// (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'MY_APP_ID',
'secret' => 'MY_SECRET',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
var_dump($user_profile); // works properly, as long as the $return below is commented out
/*
WORKS FINE UP TO HERE.
IF I ATTEMPT TO DO THE POST BELOW, I GET
OAuthException: An unknown error has occurred.
AND I AM NOT SHOWN AS LOGGED IN.
*/
$fbpost = array(
'message' => 'test message',
'name' => 'name',
'caption' => 'caption',
'link' => 'http://localhost',
'picture' => 'http://www.lov2xlr8.no/usa/bilder/63impala2.jpg'
);
$result = $facebook->api('/me/feed/', 'POST', $fbpost);
/* END OF PROBLEM SECTION */
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'manage_pages, offline_access, publish_stream, read_stream'));
}
?>
測試頁工作正常。但是,當我嘗試將其與POST測試結合使用時(如here所示),它會因未知的OAuth錯誤而失敗,並且不會發布到我的時間線上。
我在做什麼明顯錯誤?
[編輯]的FacebookApiException
添加轉儲(減去堆棧跟蹤):
object(FacebookApiException)[2]
protected 'result' =>
array (size=1)
'error' =>
array (size=3)
'message' => string 'An unknown error has occurred.' (length=30)
'type' => string 'OAuthException' (length=14)
'code' => int 1
protected 'message' => string 'An unknown error has occurred.' (length=30)
private 'string' (Exception) => string '' (length=0)
protected 'code' => int 0
protected 'file' => string 'base_facebook.php' (length=79)
protected 'line' => int 1340
[EDIT 2]
看來,它具有的所述link
元件的問題發佈對象。當我註釋掉link
時,它按預期工作併成功發佈。事實證明,Facebook顯然不會接受localhost
作爲鏈接屬性。如果我將其更改爲http://google.com
或其他一些可訪問的URL,則它可以正常工作。
「FacebookApiException」異常的完整輸出是什麼? – Gajus
上面添加了異常信息。謝謝 – user101289