2011-09-20 83 views
3

事件我在我的代碼有一個問題,當我按下創建活動什麼也沒有發生,這裏是在PHP代碼:建立在Facebook上

<?php 
$app_id = "xxxxxxxxxxxxxxxx"; 
$app_secret = "xxxxxxxxxxxxxxxx"; 
$my_url = "xxxxxxxxxxxxxxxxx"; // mainly this should be the same URL to THIS page 

$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 
. "&grant_type=client_credentials"; 
$access_token = file_get_contents($token_url); 

if(!empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time']))) { 
    $msg = "Please check your inputs!"; 
} elseif(!empty($_POST)) { 
    $url = "https://graph.facebook.com/me/events?" . $access_token; 
    $params = array(); 
    // Prepare Event fields 
    foreach($_POST as $key=>$value) 
     if(strlen($value)) 
      $params[$key] = $value; 

    // Check if we have an image 
    if(isset($_FILES) && !empty($_FILES['picture']['name'])) { 
     $uploaddir = './upload/'; 
     $uploadfile = $uploaddir . basename($_FILES['picture']['name']); 
     if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) { 
      $params['picture'] = "@" . realpath($uploadfile); 
     } 
    } 

    // Start the Graph API call 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    /* 
     Next option is only used for 
     user from a local (WAMP) 
     machine. This should be removed 
     when used on a live server! 

https://github.com/facebook/php-sdk/issues/7 

    */ 
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
    $result = curl_exec($ch); 
    $decoded = json_decode($result, true); 
    curl_close($ch); 
    if(is_array($decoded) && isset($decoded['id'])) { 
     // Event created successfully, now we can 
     // a) save event id to DB AND/OR 
     // b) show success message AND/OR 
     // c) optionally, delete image from our server (if any) 
     $msg = "Event created successfully: {$decoded['id']}"; 
    } 
} 
?> 
<!doctype html> 
<html> 
<head> 
<title>Create An Event</title> 
<style> 
label {float: left; width: 100px;} 
input[type=text],textarea {width: 210px;} 
#msg {border: 1px solid #000; padding: 5px; color: red;} 
</style> 
</head> 
<body> 
<?php if(isset($msg)) { ?> 
<p id="msg"><?php echo $msg; ?></p> 
<?php } ?> 
<form enctype="multipart/form-data" action="" method="post"> 
    <p><label for="name">Event Name</label><input type="text" name="name" value="a" /></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&nbsp;&nbsp;&nbsp; 
     <input type="radio" name="privacy_type" value="CLOSED" />Closed&nbsp;&nbsp;&nbsp; 
     <input type="radio" name="privacy_type" value="SECRET" />Secret&nbsp;&nbsp;&nbsp; 
    </p> 
    <p><input type="submit" value="Create Event" /></p> 
</form> 
</body> 
</html> 
+0

你是什麼意思「什麼都沒有發生」?你會遇到某種錯誤嗎?請提供更多信息。 –

+0

當我填寫有關事件名稱描述等信息...然後我按創建事件它沒有創建任何事情'並沒有發生錯誤 – shakyeb

回答

1

如果未返回令牌,則不會發生任何情況。當file_get_contents工作不正常時就是這種情況。爲了確保該功能將url作爲一個文件處理,你需要確保它在php.ini中的配置如下「allow_url_fopen = 1」

+0

謝謝你,你是一個天才 – shakyeb

0

看來你的表格的行動=「」是空的? ...

+1

啊哈好吧你是對的,假設是在這種情況下的行動 – shakyeb

+0

,我得到從這裏的代碼:http://www.masteringapi.com/tutorials/how-to-create-facebook-events-using-graph-api-advanced/59/ – shakyeb

+0

它太糟糕了你複製代碼而不理解它:)反正這個動作應該是處理數據的PHP文件的位置 - 在這種情況下,如果你的文件是x.php,動作應該是一樣的 - x.php –