简体   繁体   中英

How to post to a facebook page from an html form

I have an html form that has 2 text boxes and what i want to do is that when the user clicks on the submit button the content in those boxes are posted to a facebook page using PHP,here's what i have so far :

<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>

Take a look to Facebook PHP API . I haven't used it myself. It would be something like that:

<?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>';
 }

?>

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

Remember, that first you have to create a facebook app. https://developers.facebook.com/apps#sthash.U7Ms4WbT.dpuf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM