我有一個使用Facebook Graph API創建事件的PHP表單(index.php)。表單用於捕獲用戶輸入,並將這些數據回發給API以創建事件。 這與本教程中解釋的代碼相同:http://www.masteringapi.com/tutorials/how-to-create-facebook-events-using-graph-api/49/如何解析從圖形API返回的數據
這樣做的結果是,它返回剛剛創建的事件的ID。
<?php
$app_id = "APP_key";
$app_secret = "APP_secret";
$my_url = "URL";
$code = $_REQUEST["code"];
if(empty($code)) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=create_event";
echo("<script>top.location.href='" . $auth_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$event_url = "https://graph.facebook.com/me/events?" . $access_token;
?>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
</style>
</head>
<body>
<div id="inputForm">
<form enctype="multipart/form-data" action="<?php echo $event_url; ?>" method="post">
<p><label for="name">Event Name</label><input type="text" name="name" value="" /></p>
<p><label for="description">Event Description</label><textarea name="description"></textarea></p>
<p><label for="location">Location</label><input type="text" name="location" value="" /></p>
<p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
<p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m") , date("d")+1, date("Y"))); ?>" /></p>
<p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
<p>
<label for="privacy_type">Privacy</label>
<input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open
<input type="radio" name="privacy_type" value="CLOSED" />Closed
<input type="radio" name="privacy_type" value="SECRET" />Secret
</p>
<p><input type="submit" value="Create Event" /></p>
</form>
</div>
<?php
此代碼的偉大工程,但我試圖修改此代碼,這樣,index.php文件數據發佈到$ EVENT_URL創建事件,但將您重定向到一個顯示一條消息,如確認頁面:
「活動成功創建!點擊此處訪問您的活動」,然後點擊「此處」會將您的活動重定向到Facebook。
有關如何做到這一點的任何想法?
謝謝!我通過$ access_token變量作爲隱藏輸入。在「event_submitter.php」上,使用cURL POST回到Facebook: $ event_url =「https://graph.facebook.com/me/events?」 。 $的access_token; $ ch = curl_init(); curl_setopt($ ch,CURLOPT_URL,$ event_url); curl_setopt($ ch,CURLOPT_USERAGENT,「I-am-browser」); curl_setopt($ ch,CURLOPT_HEADER,0); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ ch,CURLOPT_TIMEOUT,10); $ response = curl_exec($ ch); curl_close($ ch); var_dump(json_decode($ response,true)); –
但是,我看到它返回一個空的「data []」數組。 cURL的使用方式有問題嗎? –
您確定自己的網址正確嗎?你應該在那裏有一個'access_token ='?在你連接你的'$ access_token'之前,它看起來好像有';'。我沒有看到你告訴cURL將其作爲POST提交或在你的cURL調用中包含你的POST數據。如果是這種情況,您正在進行GET調用,如果您的配置文件當前沒有任何事件,您將收到空的數據。 – cpilko