Location: PHPKode > scripts > Twitter Promoter > promoter.php
<?php

interface promoter_twitter {

    public function unfollow();

    public function prepere_users();

    public function getFollowers($username);

    public function begin_follow();

    public function DOMElement_getOuterHTML($document, $element);

    public function Re_check_folowers();

    public function string_getInsertedString($long_string, $short_string, $is_html=false);
}

class promoter implements promoter_twitter {

    public $param = array();
    private $curl;
    public $tofollow = array();
    public $last_follow;
    public $not_followed = array();

    public function __construct($p=array()) {
        $this->param = $p;
        $this->curl = curl_init();
    }

    public function unfollow() {

        // Since BASIC AUTORIZATION DOesn't support for this ((
        // THis Method Will work Only With OAuthorization on twitter So if want you can modify this for OAuth

        /*
          foreach ($this->not_followed as $user) {
          $url = 'http://twitter.com/friendships/destroy/' . $user . '.json';

          curl_setopt($this->curl, CURLOPT_URL, $url);
          curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 2);
          curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($this->curl, CURLOPT_POST, 1);
          curl_setopt($this->curl, CURLOPT_POSTFIELDS, false);
          curl_setopt($this->curl, CURLOPT_VERBOSE, 1);
          curl_setopt($this->curl, CURLOPT_NOBODY, 0);
          curl_setopt($this->curl, CURLOPT_HEADER, 0);
          curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($this->curl, CURLOPT_USERPWD, "" . $this->param["login"] . ":" . $this->param["password"] . "");
          curl_exec($this->curl);
          }
         */



        return true;
    }

    public function Re_check_folowers() {

        if (isset($this->param["file_p"])) {
            $chck = file($this->param["file_p"] . "do_not" . $this->param["login"] . ".txt");

            foreach ($chck as $user) {

                $url = "http://twitter.com/friendships/show.json?source_screen_name=" . $this->param["login"] . "&target_screen_name=" . $user;
                curl_setopt($this->curl, CURLOPT_URL, $url);
                curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 2);
                curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
                $buffer = curl_exec($this->curl);
                if (isset(json_decode($buffer)->relationship)) {
                    $buffer = json_decode($buffer)->relationship;

                    if (is_array($buffer) || is_object($buffer)) {
                        foreach ($buffer as $r) {
                            if (!$r->following)
                                $this->not_followed[] = $r->screen_name;
                        }
                    }
                }
            }
        }
    }

    public function begin_follow() {

        $do_not = array();
        $finished = array();
        if (isset($this->param["file_p"]) && file_exists($this->param["file_p"] . "do_not" . $this->param["login"] . ".txt")) {
            $do_not = file($this->param["file_p"] . "do_not" . $this->param["login"] . ".txt");
        }



        $limit = 0;

        foreach ($this->tofollow as $follow) {



            if (in_array(trim($follow . "\n"), $do_not, 1) == false && $limit < $this->param["limit"]) {

                $limit++;
                $url = 'http://api.twitter.com/1/friendships/create/' . $follow . '.xml?follow=true';

                curl_setopt($this->curl, CURLOPT_URL, $url);
                curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 2);
                curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($this->curl, CURLOPT_POST, 1);
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, false);
                curl_setopt($this->curl, CURLOPT_VERBOSE, 1);
                curl_setopt($this->curl, CURLOPT_NOBODY, 0);
                curl_setopt($this->curl, CURLOPT_HEADER, 0);
                curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);


                curl_setopt($this->curl, CURLOPT_USERPWD, "" . $this->param["login"] . ":" . $this->param["password"] . "");


                if ($this->param["if_has"] > 0) {
                    if ($this->getFollowers($follow) >= $this->param["if_has"]) {
                        $buffer = curl_exec($this->curl);
                        echo "<h1>  Chekceed</h1>";
                    }
                } else {
                    $buffer = curl_exec($this->curl);
                }
                if (!empty($buffer)) {


                    $finished[] = $follow;
                }
            }
        }



        if (isset($this->param["file_p"])) {
            foreach ($finished as $f) {
                file_put_contents($this->param["file_p"] . "do_not" . $this->param["login"] . ".txt", $f . "\n", FILE_APPEND);
            }
        }
    }

    public function prepere_users() {

        $url = "http://search.twitter.com/search.json?q=" . $this->param["equal"];


        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        $buffer = json_decode(curl_exec($this->curl)); // ñîõðàíÿåì îòâåò

        if (is_array($buffer) || is_object($buffer)) {
            foreach ($buffer as $p => $ob) {

                if (is_array($ob) || is_object($ob)) {
                    foreach ($ob as $pa) {
                        $this->tofollow[] = $pa->from_user;
                    }
                }
            }
        }
    }

    public function string_getInsertedString($long_string, $short_string, $is_html=false) {
        if ($short_string >= strlen($long_string)

            )return false;
        $insertion_length = strlen($long_string) - strlen($short_string);
        for ($i = 0; $i < strlen($short_string); ++$i) {
            if ($long_string[$i] != $short_string[$i]

                )break;
        }
        $inserted_string = substr($long_string, $i, $insertion_length);
        if ($is_html && $inserted_string[$insertion_length - 1] == '<') {
            $inserted_string = '<' . substr($inserted_string, 0, $insertion_length - 1);
        }
        return $inserted_string;
    }

    public function DOMElement_getOuterHTML($document, $element) {
        $html = $document->saveHTML();
        $element->parentNode->removeChild($element);
        $html2 = $document->saveHTML();
        return $this->string_getInsertedString($html, $html2, true);
    }

    public function getFollowers($username) {
        $x = file_get_contents("http://twitter.com/" . $username);
        $doc = new DomDocument;
        @$doc->loadHTML($x);
        $ele = $doc->getElementById('follower_count');
        $innerHTML = preg_replace('/^<[^>]*>(.*)<[^>]*>$/', "\\1", $this->DOMElement_getOuterHTML($doc, $ele));
        return $innerHTML;
    }

    public function __destruct() {
        curl_close($this->curl);
    }

}

?>
Return current item: Twitter Promoter