【问题标题】:How to post to a facebook page from an html form如何从 html 表单发布到 Facebook 页面
【发布时间】:2014-10-19 09:38:05
【问题描述】:

我有一个包含 2 个文本框的 html 表单,我想要做的是,当用户单击提交按钮时,这些框中的内容会使用 PHP 发布到 facebook 页面,这是我目前所拥有的:

<html>
<title>Post to fb</title>

<form  action="" method="post">
Name:<input type="text" name="input">
description : <input type="text" name="description">
<input type="submit" value="Submit">
</form>

<?php

$server = "localhost";
 $database = "demo";
 $user = "root";
 $password = "";
$db_name="demo"; // Database name 

if($_POST["input"] )
{
session_start();     
    $conn=  @mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("website",$conn);
    $sql="insert into post (name,description)values('$_POST[name]','$_POST[description]')";
    $result=mysql_query($sql,$conn) or die(mysql_error());        

}

?>

</html>

【问题讨论】:

    标签: php html mysql facebook post


    【解决方案1】:

    看看Facebook PHP API。 我自己没用过。应该是这样的:

    <?php
     include_once 'src/facebook.php';
    
     $appId = 'APPID_FROM_FACEBOOK';
     $secret = 'APPSECRET_FROM_FACEBOOK';
     $returnurl = 'YOUR_PAGE_URL';
     $permissions = 'manage_pages, publish_stream';
    
     $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
    
     $fbuser = $fb->getUser();
    
     if($fbuser){
    
        if(isset($_POST['msg']) and $_POST['msg']!=''){
            try{
                $message = array(
                    'message' => $_POST['msg']
                );
                $posturl = '/'.$_POST['pageid'].'/feed';
                $result = $fb->api($posturl,'POST',$message);
                if($result){
                    echo 'Successfully posted to Facebook Wall...';
                }
            }catch(FacebookApiException $e){
                echo $e->getMessage();
            }
        }
    
        try{
            $qry = 'select page_id, name from page where page_id in (select page_id from page_admin where uid ='.$fbuser.')';
            $pages = $fb->api(array('method' => 'fql.query','query' => $qry));
    
            if(empty($pages)){
                echo 'The user does not have any pages.';
            }else{
                echo '<form action="" method="post">';
                echo 'Select Page: <select name="pageid">';
                foreach($pages as $page){
                    echo '<option value="'.$page['page_id'].'">'.$page['name'].'</option>';
                }
                echo '</select>';
                echo '<br />Message: <textarea name="msg"></textarea>';
                echo '<br /><input type="submit" value="Post to wall" />';
                echo '</form>';
            }
    
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }
    
     }else{
        $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
        echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
     }
    
    ?>
    

    来源:http://www.detailsinn.com/c7f76878ca736e015b7a94bf3036136c1357649641-article-posting-in-facebook-page-wall-from-a-php-application.html

    请记住,首先您必须创建一个 facebook 应用程序。 https://developers.facebook.com/apps#sthash.U7Ms4WbT.dpuf

    【讨论】:

      猜你喜欢
      • 2010-09-17
      • 2011-11-10
      • 2011-07-10
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2011-08-11
      相关资源
      最近更新 更多