Post to Facebook page using php

I have a script to post to facebook, but I'm having problems with how the data is posted to facebook.

Since this might help others, this is the complete code

<?php
require 'facebook.php';
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 0;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = 0;

$pageId   = "XXXXXXXX";
$permSess = "XXXXXXXXXXXXXXXX";

$facebook = new Facebook(array(
    "appId" => "XXXXXXXXXX",
    "secret" => "XXXXXXXXXX",
    "cookie" => true
));

$page = $facebook->api("/{$pageId}");
?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

    <head>
        <title>Post to Page Wall</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
        />
        <meta name="description" content="Post to page wall" />
        <style>
            body {
                font-family:'Lucida Grande', Verdana, Arial, sans-serif;
                background-color: #f2f2f2;
            }
            h1 a {
                text-decoration: none;
                color: #3b5998;
            }
            h1 a:hover {
                text-decoration: underline;
            }
            form {
                border: 1px solid #eee;
                padding: 20px;
                width: 550px;
            }
            textarea, select, input, label {
                width: 500px;
                border: 1px solid #ddd;
                height: 20px;
                clear: both;
                margin: 10px;
            }
            textarea {
                height: 100px;
            }
            label {
                border: none;
                font-weight: bold;
            }
            input#submit {
                width: 100px;
            }
        </style>
    </head>

    <body>
        <h1>Post to Friend Wall</h1>
        <?php if(isset($_POST[ 'submit'])) { $link=$
        _POST[ 'link']; $message=$ _POST[ 'message']; $attachment=a rray($message,$link
        ); $rest=$ facebook->api(array( "uid" => $pageId, "method" => "stream.publish", "access_token"
            => $permSess, "message" => $message )); } ?>
            <form id="Wall" name="Wall"
            method="post">
                <label for="URL">URL:</label>
                <input id="link" name="link">
                <label for="Message">Message:</label>
                <textarea id="message" name="message"></textarea>
                <input type="submit" name="submit" id="submit" value="Send!">
            </form>
    </body>

</html>

      

Problem:

I used the example posted here and using this example, the links provided in the url field where you post to facebook is the same as if you would do it yourself (with an image and site description), but using my code, I cannot do it the same way.

Incl. How to post to Facebook fan site wall using PHP and Open Graph API ? Some users left an answer, but my case doesn't work.

The difference between my code and the code on another site is the "$ attachment" part of the code

<?php
if (isset($_POST['submit'])) {
    $sendTo  = $_POST['friend'];
    $link    = $_POST['link'];
    $message = $_POST['message'];

    // all options: https://stackoverflow.com/questions/691425/how-do-you-post-to-the-wall-on-a-facebook-page-not-profile
    $attachment = array(
        'message' => $message,
        'link' => $link
    );

    if ($result = $facebook->api("/$sendTo/feed/", 'post', $attachment)) {
        $feedbackMessage = "Message sent to friend $sendTo";
    } else {
        $feedbackMessage = "Oops something went wrong";
    }
}
?>

      

So ... the question is how to properly post on a Facebook page without losing any advantage or formatting with the script.

Thank you in advance

+3


source to share


1 answer


This will allow you to post to the facebook page and keep the format, I got what you mean!



<?php 
if(isset($_POST['submit'])) {
            $link = $_POST['link'];
            $message = $_POST['message'];



$attachment = array(
        'access_token' => $permSess,
        'message' => $message,
        'link' => $link

    );
if(    $result = $facebook->api(
        "/".$pageId."/links",
        'post',
        $attachment
    )){
                $feedbackMessage = "Message sent to friend $sendTo";
            } else {
                $feedbackMessage = "Oops something went wrong";
            } 

}
?>

      

+3


source







All Articles