. */ /* * * Config * * */ $scrapeUrl = "http://www.example.com"; // Url to scrape $userAgent = "Twitter Bot 0.1 http://andrewjaswa.com/twitter-bot/"; // User agent. You should set up a page to infom people of what you are doing with your bot. $dlRange = "0"; // How much would you like to download in bytes? Leave 0 for everthing. put in 123-333 fir a range. Doesn't work on all servers see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.5 $up = "user:password"; // The twitter user and password you want to update. Format: user:password $sUrl = "http://shortna.me/"; // url that gets appented to your tweet. $statusUrl = "http://twitter.com/statuses/user_timeline.json?count=1"; // I'm using json because its easy and fast. $updateUrl = "http://twitter.com/statuses/update.json"; /* * Grab the site */ if (function_exists('curl_init') && $scrapeUrl) { $ch = curl_init($scrapeUrl); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); curl_setopt($ch, CURLOPT_GET, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RANGE, $dlRange); $content = curl_exec($ch); curl_close($ch); if ($content !== false && $content !== '' ){ /* * this is where your tweet is. * */ //this finds the content you want to pull out of the page in this case it will grab the title of the html document. preg_match("/<\s*title[^>]*>(.+)<\/title/", $content, $matches); $tweet = $matches[1]." - ".$sUrl; $session = curl_init($statusUrl); curl_setopt($session, CURLOPT_USERAGENT, $userAgent); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); curl_setopt($session, CURLOPT_FAILONERROR, true); curl_setopt($session, CURLOPT_GET, true); curl_setopt($session, CURLOPT_USERPWD, $up); curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($session, CURLOPT_TIMEOUT, 10); $stream = curl_exec($session); curl_close($session); $fromTwitter = json_decode($stream, true); $curlPost = "status=".$tweet; if ($tweet != $fromTwitter[0]['text']){ $session = curl_init($updateUrl); curl_setopt($session, CURLOPT_USERAGENT, $userAgent); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); curl_setopt($session, CURLOPT_FAILONERROR, true); curl_setopt($session, CURLOPT_POST, true); curl_setopt($session, CURLOPT_USERPWD, $up); curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($session, CURLOPT_TIMEOUT, 10); curl_setopt($session, CURLOPT_POSTFIELDS, $curlPost); $stream = curl_exec($session); curl_close($session); } } elseif( $content === false ) { // fail message echo "The bot failed. "; } elseif( $content == '' ) { // empty echo "No content was returned."; } } ?>