だから私はこの仕事をしようとして髪を引き裂いています。私は既存の質問を見てスタックオーバーフローをずっと見ていて、これを見つけました:Facebook:ページの問題としてページに投稿(アクセストークン/ php)しかし、それでも私の問題は解決していないようです。
cron ジョブを使用して、Facebook ページに毎日投稿しようとしています。認証に問題があります。これが私のコードです:
//Post to Facebook
//Variables
$app_id = "...";
$app_secret = "...";
$page_id = "...";
$my_url = "http://.../";
$access_token = "taken from page access token (developers.facebook.com/tools/explorer/)";
//Create the facebook object
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//Get the access token using Facebook Graph API
//This gives permission to post to the wall
$token_url="https://graph.facebook.com/oauth/access_token?client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&code=" . $access_token
. "&redirect_uri=" . $my_url;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$user_access_token = $params['access_token'];
//Get a list of pages and loop through
//If one matches one in the variables above, that's the one
//We're posting to
$attachment_1 = array(
'access_token' => $user_access_token
);
$result = $facebook->api("/me/accounts", $attachment_1);
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
//Write to the Page wall
try {
$attachment = array(
'access_token' => $page_access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/me/feed','POST', $attachment);
} catch(Exception $e) {
//Send me an email
...
mail($to, $subject, $message, $headers);
}
これは、ブラウザでスクリプトにアクセスすると機能しますが (Facebook セッションを使用していると仮定します)、cron ジョブによってトリガーされた場合は機能しません。
助けていただければ幸いです。ありがとう。