Created
July 11, 2016 09:31
-
-
Save JeansBolong/f6a9e6590a52a149f664303f9a08f7cb to your computer and use it in GitHub Desktop.
Update your Twitter status using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Send a Tweet to your Twitter account. | |
tweetThis('my_user_name', 'my_password', 'my_tweet_message'); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function tweetThis($strUsername = '', $strPassword = '', $strMessage = '') { | |
if (function_exists('curl_init')) { | |
$twitterUsername = trim($strUsername); | |
$twitterPassword = trim($strPassword); | |
if (strlen($strMessage) > 140) { | |
$strMessage = substr($strMessage, 0, 140); | |
} | |
$twitterStatus = htmlentities(trim(strip_tags($strMessage))); | |
if (!empty($twitterUsername) && !empty($twitterPassword) && !empty($twitterStatus)) { | |
$strTweetUrl = 'http://www.twitter.com/statuses/update.xml'; | |
$objCurlHandle = curl_init(); | |
curl_setopt($objCurlHandle, CURLOPT_URL, "$strTweetUrl"); | |
curl_setopt($objCurlHandle, CURLOPT_CONNECTTIMEOUT, 2); | |
curl_setopt($objCurlHandle, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($objCurlHandle, CURLOPT_POST, 1); | |
curl_setopt($objCurlHandle, CURLOPT_POSTFIELDS, "status=$twitterStatus"); | |
curl_setopt($objCurlHandle, CURLOPT_USERPWD, "$twitterUsername:$twitterPassword"); | |
$result = curl_exec($objCurlHandle); | |
$arrResult = curl_getinfo($objCurlHandle); | |
if ($arrResult['http_code'] == 200) { | |
echo 'Your Tweet has been posted'; | |
} | |
else { | |
echo 'Could not post your Tweet to Twitter.'; | |
} | |
curl_close($objCurlHandle); | |
} | |
else { | |
echo('Missing required information to submit your Tweet.'); | |
} | |
} | |
else { | |
echo('Curl Extension is not installed.'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment