Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need a php script to publish to facebook

Posted on 2009-07-09
7
Medium Priority
?
1,200 Views
Last Modified: 2012-05-07
Hello, I have been reading and reading the facebook api docs, but man I just want to post to my facebook wall from a form on the back end of my site... any one have a script handy?  Thanks for the help...

Jeremy
0
Comment
Question by:jeremyBass26
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 750 total points
ID: 24823572
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 750 total points
ID: 24823596
Just a sidebar note - my colleagues, some of whom are pretty well wired into the PHP community, have indicated that the interfaces and documentation are not (how shall we say this) flawless, so be prepared for surprises along the way.  Best of luck, ~Ray
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 24824736
@RQuadling I'm sorry I should have stated that the Facebook docs I have been pouring thru were the wiki you point out... but, @Ray_Paseur, like you say, those docs are far from flawless.  I'd almost measure in miles how unhelpful they have been so far lol... just a little fun is the discussion for the page

http://wiki.developers.facebook.com/index.php/Talk:PHP

One thing is for sure the, twitter docs are super easy and better to work with... sigh.... Well hopefully I'll get it sorted soon, or someone pops up with that basic how-to...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:jeremyBass26
ID: 24825552
talk about a surprise...

If youre a business you can't get an api key unless you have a profile.  If youre a business you cant have a profile. rise repete...

So with that you can't do any of the apps as a business.  "If you create a profile for your business, your account may be disabled for violating our Terms of Use."

What is with that... I don't know what to do here..  I DONT want to have the business I work for and all the business clients tied to my personal account.

Does any one here know what to do?  This is sad to work with twitter it's so much more, well normal... I popped this test script out in 5-10 mins (below)...

/me hit's head on wall...

thanks for the/any help

Jeremy
function postToTwitter($username,$password,$message){
 
    $host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
 
    $result = curl_exec($ch);
    // Look at the returned header
    $resultArray = curl_getinfo($ch);
 
    curl_close($ch);
 
    if($resultArray['http_code'] == "200"){
         $twitter_status='Your message has been sended! <a href="http://twitter.com/'.$username.'">See your profile</a>';
    } else {
         $twitter_status="Error posting to Twitter. Retry";
    }
	return $twitter_status;
}
 
$twitter_username ='digitalbarn';
$twitter_psw ='XXXXXXXXXXXX';
/* ---------------------------------------- */
 
/* Don't change the code belove
/* ---------------------------------------- */
//require('twitterAPI.php');
if(isset($_POST['twitter_msg'])){
	$twitter_message=$_POST['twitter_msg'];
	if(strlen($twitter_message)<1){
		 $error=1; 
	} else {
	 	$twitter_status=postToTwitter($twitter_username, $twitter_psw, $twitter_message); 
	}
}
 
 
 
 
if(isset($_POST['twitter_msg']) && !isset($error)){
echo '<div class="msg">'.$twitter_status.'</div>';
} else if(isset($error)){
echo '<div class="msg">Error: please insert a message!</div>';
}
echo'<p><strong>What are you doing?</strong></p>
<form action="'.$urls.'" method="post">
<input name="twitter_msg" type="text" id="twitter_msg" size="40" maxlength="140"/>
<input type="submit" name="button" id="button" value="post" />
</form>';

Open in new window

0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 24844197
Ok.. so I'm seeing that face book sucks big time from a dev point of view... and is only half assing it on business end of things... but there is no way to do the basic function that I show in the twitter example.. and do to that, I'd think that my question will not be solved in the way I was hoping... being that... What do you two want to do about the points here... I'll solve it out and split the points if you two are cool with that so it doesn't just sit here...

Thanks for the help...
Jeremy
0
 
LVL 4

Author Closing Comment

by:jeremyBass26
ID: 31601858
Hey I just got rid of it.. thanks for the words... Cheers
Jeremy
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24849246
Yeah, Twitter is a lot better for developer interface than FB.  Here is my Twitter class.  Best to all, ~Ray
<?php // class_Twitter.php - NPC TWITTER AUTO-FEED
error_reporting(E_ALL);
 
//
// SEND AN AUTOMATED TWEET
// USAGE EXAMPLE
// require_once('class_Twitter.php');
// $text = 'Hello World';
// $t = new Twitter;
// $t->tweet($text);
// unset ($t);
//
 
// DO NOT RUN THIS SCRIPT STANDALONE (HAS PASSWORD)
if (count(get_included_files()) < 2) { header("HTTP/1.1 301 Moved Permanently"); header("Location: /"); exit; }
 
class Twitter
{
    private $ch;
    private $you  = "You@Your.org";
    private $user;
    private $pass;
    private $test;
    private $host = "http://twitter.com/";
    private $done; // ALREADY TWEETED?
    public  $res;
 
    public function __construct()
    {
        $this->done = FALSE; // DEFAULT - NOT ALREADY TWEETED
        $this->test = FALSE;
        $this->user = "Twitter_User_Name";
        $this->pass = "Twitter_User_Password";
        $this->ch   = curl_init();
        curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_USERPWD, "$this->user:$this->pass");
        curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($this->ch, CURLOPT_POST, 1);
    }
 
    public function __destruct()
    {
        curl_close($this->ch);
    }
 
    public function test()
    {
        $this->test = TRUE;
    }
 
    private function already_tweeted($message)
    {
        $msg = trim($message);
        $dat = date('Y-m-d');
        $md5 = md5($msg . $dat);
        $msg = mysql_real_escape_string($msg);
        $sql = "SELECT id FROM twitterLog WHERE thash = \"$md5\" ORDER BY id DESC LIMIT 1";
        $res = mysql_query($sql);
        if (!$res) die(mysql_error());
        $num = mysql_num_rows($res);
        if ($num) return TRUE;
        $sql = "INSERT INTO twitterLog (tdate, thash, tweet) VALUES ( \"$dat\", \"$md5\", \"$msg\" )";
        $res = mysql_query($sql);
        if (!$res) die(mysql_error());
        return FALSE;
    }
 
    public function tweet($message)
    {
        if(strlen($message) < 1) return FALSE;
        if ($this->already_tweeted($message)) $this->done = TRUE;
 
        if ($this->test)
        {
            $msg = '';
            if ($this->done) $msg .= "ALREADY DONE ";
            $msg .= "TWEET: $message";
            mail($this->you, 'What We Could Have Tweeted', $msg);
            return TRUE;
        }
 
        if ($this->done) return TRUE;
 
        $this->host .= "statuses/update.xml?status=". urlencode(stripslashes(urldecode($message)));
        curl_setopt($this->ch, CURLOPT_URL, $this->host);
        $xxx       = curl_exec($this->ch);
        $this->res = curl_getinfo($this->ch);
        if ($this->res['http_code'] == 0)   return TRUE;
        if ($this->res['http_code'] == 200) return TRUE;
        return FALSE;
    }
}
?>

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question