Publishing Twitter status in a group (or page) in VKontakte

or_die, the errors are gone in the logs, but
Nothing is posted on the page…
After executing the script, it displays
“Update” message. to the cookie file and
tweets cost 777 rights, on the script itself
644 to both cookies and tweets
after executing the script
being recorded.

There is a script, with it you can publish twitter statuses in VKontakte automatically, but I would like to publish not on the profile wall, but in a group or official page, for example: http://vkontakte.ru/of_stranica, how should the script change?
P.S. Here is the script itself:

<?php
 /* 
    tvkontakte 0.1i
    vkontakte.ru status updater from twitter
    made by Vladimir Smirnoff
    http://orl.sumy.ua
    [email protected]
    improved by Kichrum
    http://Kichrum.org.ua
 */
#  - !
define ('TWITTER_USERNAME','MyLogin'); // MyLogin      Twitter
define ('VKONTAKTE_LOGIN','[email protected]'); // [email protected]    E-mail  Vkontakte.ru
define ('VKONTAKTE_PASSWORD','12345'); // gfhjkm       Vkontakte.ru
define ('PREPEND_WORD','Twitter.com/user'); // Twitter.com/user   ,     :  : 20    = 160  vkontakte - 140  twitter 
#  .
#     :)
#   UserAPI
class vkuserapi {
    private $curlh = null;
    public $uid = null;
    private $sid = null;
    public function __construct ($login,$password) {
        $this->curlh = curl_init();
        $request = 'http://login.userapi.com/auth?site=2&login=force&pass='.urlencode($password).'&email='.urlencode($login);
        curl_setopt($this->curlh, CURLOPT_URL,$request);
        curl_setopt($this->curlh, CURLOPT_FAILONERROR, true);
        curl_setopt($this->curlh, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($this->curlh, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curlh, CURLOPT_POST, false); 
        curl_setopt($this->curlh, CURLOPT_HEADER, true);
        $auth_result = curl_exec($this->curlh);
        if (preg_match("/remixmid=(\d+)/", $auth_result, $matches)) $this->uid = $matches[1];
        if (preg_match("/.*;sid=(\w*)/", $auth_result, $matches)) $this->sid = $matches[1];
    }
    public function call ($func,$params) {
        curl_setopt($this->curlh, CURLOPT_HEADER, false);
        $request = 'http://userapi.com/data?act='.$func.'&'.http_build_query ($params).'&sid='.$this->sid;
        curl_setopt($this->curlh, CURLOPT_URL,$request);
        $result = curl_exec($this->curlh);
        return json_decode($result);
    }
    public function __destruct () {
        $request = 'http://login.userapi.com/auth?login=logout&site=2&sid='.$this->sid;
        curl_setopt($this->curlh, CURLOPT_URL,$request);
        curl_exec($this->curlh);
        curl_close ($this->curlh);
    }
}
#     RSS-  Twitter.com
$rss = simplexml_load_file('http://twitter.com/statuses/user_timeline/'.TWITTER_USERNAME.'.rss');
#   
foreach ($rss->channel->item as $status) {
    if (strpos($status->title,'@')) continue; //    Twitter
    $last_status_twitter = str_replace (TWITTER_USERNAME.': ','',$status->title); break;
}
#       vtweet.txt
$f=fopen(dirname(__FILE__).'/vtweet.txt','r');
$last_status_vkontakte = fread($f,512);
fclose($f);
#         ,      
if (($last_status_vkontakte != $last_status_twitter) && ($last_status_twitter != NULL)) {
$vkontakte = new vkuserapi (VKONTAKTE_LOGIN,VKONTAKTE_PASSWORD); // 
$vkontakte->call ('set_activity',array('ts'=>time(),'text'=>PREPEND_WORD.$last_status_twitter)); //  
unset ($vkontakte); //   Vkontakte.ru
$f=fopen(dirname(__FILE__).'/vtweet.txt','w');
fwrite($f,$last_status_twitter); // ""    
fclose($f);
echo 'Updatetd.';
}
else echo 'No update needed.'; //    ,   .
#   .
?>

or_die, this script will be used on both VPS and regular hosting. (I had to edit the post because I can’t add any more comments)


Answer 1

Rewritten, it seems to work 🙂

<?php
##################
$vk_login = '[email protected]';
$vk_password = 'vkontakte pass';
$vk_page = 'vk puplic page';
$tw_login = 'tw login';
##################
$cookie_file = 'cookies.txt';
$tweet_file  = 'vtweet.txt';
$mode = 1; // 1 -     0 -    
##################
VK_Login($vk_login, $vk_password);
$f=fopen(dirname(__FILE__).'/'.$tweet_file,'r');
$rss = simplexml_load_file('http://twitter.com/statuses/user_timeline/'.$tw_login.'.rss');
#   
foreach ($rss->channel->item as $status) {
    if (strpos($status->title,'@')) continue; //    Twitter
    $last_status_twitter = str_replace ($tw_login.': ','',$status->title);
    $last_status_vkontakte = fread($f,512);
    if (($last_status_vkontakte != $last_status_twitter) && ($last_status_twitter != NULL)) {
        VK_Page_Sending($last_status_twitter);
        $f=fopen(dirname(__FILE__).'/'.$tweet_file,'w');
        fwrite($f,$last_status_twitter);
        echo ($mode == 1) ? "Update" : "";
    }
    else
    {
        echo ($mode == 1) ? 'No update needed.' : "";
    }
    if($mode == 1) break;
}
fclose($f);
function VK_Login($vk_login,$vk_password)
{
    global $cookie_file;  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_URL, "http://vkontakte.ru/login.php?m=1&email={$vk_login}&pass={$vk_password}");  
    curl_setopt ($ch, CURLOPT_USERAGENT, "Opera/9.64 (Windows NT 5.1; U; ru) Presto/2.1.1");  
    $headers = array(  
    'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',    'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8',  
    'Content-Type: application/x-www-form-urlencoded',  
    'Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1',  
    'Accept-Encoding: deflate',  
    'Cookie2: $Version=1',  
    'Connection: Keep-Alive, TE',  
    'TE: deflate, gzip, chunked, identity, trailers');  
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);  
    curl_setopt($ch, CURLOPT_HEADER, 0);  
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);  
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
    curl_setopt($ch,CURLOPT_ENCODING , "gzip");
    curl_exec($ch); 
    curl_close($ch);
}
function VK_Page_Sending($message)
{
    global $vk_page, $cookie_file;
    static $hash, $public_id;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);  
    if(!isset($hash) && !isset($public_id))
    {
        curl_setopt($ch, CURLOPT_URL, 'http://vkontakte.ru/'.$vk_page);
        $answer = curl_exec($ch);
        preg_match('#"post_hash":"(.*?)"#',$answer,$hash_array);
        preg_match('#"public_id":(\d+)#i',$answer,$public_id_array);
        $hash = $hash_array[1];
        $public_id = $public_id_array[1];
    }
    curl_setopt($ch, CURLOPT_URL, 'http://vkontakte.ru/al_wall.php');
    $post_send = array(
                        'act' => 'post',
                        'al' => 1,
                        'hash' => $hash,
                        'message' => $message,
                        'to_id' => '-'.$public_id,
                        'type' => 'all'
                        );
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_send));
    curl_exec($ch);
    curl_close($ch);
}
?>