VK api class загрузка фото и пост на стену

Создание Desktop-приложений, виджеты ВКонтакте для сайтов и публичных страниц, OAuth 2.0, Open API, Standalone
Ответить
Slyfoxleo
Сообщения: 3
Зарегистрирован: 16 ноя 2012, 01:55

VK api class загрузка фото и пост на стену

Сообщение Slyfoxleo »

Код: Выделить всё

<?php class classVkApi {      private $user_id;  private $access_token;  private $user_agent;    public function __construct($user_id, $access_token, $imagePath)  {       $this->user_id = $user_id;    $this->access_token = $access_token;    $this->user_agent = $_SERVER['HTTP_USER_AGENT'];    $this->imagePath = $imagePath;  }      public function api($method, $param) {    $res = $this->curlPost("https://api.vkontakte.ru/method/$method?$param&access_token=$this->access_token", array());        return json_decode($res);  }      public function wallMessage($msg, $url) {      if (is_int($msg[0]))      $msg .= '%20'.$msg;                $msg = str_replace(' ', '%20', $msg);        $server = $this->api("wall.post","owner_id=$this->user_id&message=$msg&attachments=$url");     //print_r($server);      }    public function uploadImg($aid, $url, $text) {     $text = str_replace(' ', '%20', $text);            $data = array("file1"=>"@".$this->saveImg($url));        $server = $this->api("photos.getUploadServer","aid=$aid");       $res = $this->curlPost($server->response->upload_url, $data);    $upload = json_decode($res);         $save = $this->api("photos.save", "caption=$text&aid={$upload->aid}&server={$upload->server}&photos_list={$upload->photos_list}&hash={$upload->hash}");        return $save->response[0]->id;      }    public function saveImg($url) {    $upload_dir = $this->imagePath;    $name = 'image.jpg';    $file = file_get_contents($url);      $openedfile = fopen($upload_dir.$name, "w");    fwrite($openedfile, $file);    fclose($openedfile);        return $upload_dir.$name;  }      private function curlPost($url, $data=array())    {         if ( ! isset($url))        {            return false;        }                                          $ch = curl_init();        curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);        curl_setopt($ch, CURLOPT_TIMEOUT, 10);        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);        curl_setopt($ch, CURLOPT_URL, $url);              if (count($data) > 0)        {            curl_setopt($ch, CURLOPT_POST, true);            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);        }         $response = curl_exec($ch);         curl_close($ch);                return $response;        }  }  ?>
Выдает {"code":"0"}
Помогите с классом, может я не правильно переменные передаю.
Slyfoxleo
Сообщения: 3
Зарегистрирован: 16 ноя 2012, 01:55

Re: VK api class загрузка фото и пост на стену

Сообщение Slyfoxleo »

вообще существует способ загрузить фото в альбом, без использования api?
тут есть вот такой вот класс

Код: Выделить всё

<? class vk_auth{     private $email = '';    private $pwd = '';    private $phone = '';    private $sleeptime = 1;    private $minicurl;      function __construct()    {        $this->email = VKEMAIL;        $this->pwd = VKPWD;        $this->phone = VKPHONE;        $this->sleeptime = SLEEPTIME;        $this->minicurl = new minicurl(TRUE, COOKIES_FILE, 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1');    } /** public auth functions*/     public function check_auth()    {        if (strlen($this->phone) != 4)        {            $this->put_error_in_logfile('4 LAST DIGITS from phone!!!');            exit();        }         if($this->need_auth())        {            if(!$this->auth())            {                $this->put_error_in_logfile('Not authorised!');                return FALSE;            }        }         return TRUE;    } /** public posting functions */     public function post_to_user($user_id, $message, $friends_only = FALSE)    {        // check_auth() - так ли тут нужно? по-моему, нет                if (!is_numeric($user_id))        {            $this->put_error_in_logfile('$user_id - only numbers!');            return FALSE;        }         $hash = $this->get_hash('id' . $user_id);        if (empty($hash))        {            $this->put_error_in_logfile('JS-Field "post_hash" not found!');            return FALSE;        }         if(!$this->post_to_wall_query($hash, $user_id, $message, FALSE, $friends_only, 'feed'))        {            $this->put_error_in_logfile('Message not posted!');            return FALSE;        }         return TRUE;    }     public function post_to_group($group_id, $message, $official = FALSE)    {        if (!is_numeric($group_id))        {            $this->put_error_in_logfile('$group_id - only numbers!');            return FALSE;        }         $hash = $this->get_hash('club' . $group_id);        if (empty($hash))        {            $this->put_error_in_logfile('JS-Field "post_hash" not found!');            return FALSE;        }         $group_id = '-' . $group_id;         if(!$this->post_to_wall_query($hash, $group_id, $message, $official, FALSE))        {            $this->put_error_in_logfile('Message not posted!');            return FALSE;        }         return TRUE;    }     public function post_to_public_page($page_id, $message)    {        if (!is_numeric($page_id))        {            $this->put_error_in_logfile('$page_id - only numbers!');            return FALSE;        }         $hash = $this->get_hash('public' . $page_id);        if (empty($hash))        {            $this->put_error_in_logfile('JS-Field "post_hash" not found!');            return FALSE;        }         $page_id = '-' . $page_id;         if(!$this->post_to_wall_query($hash, $page_id, $message))        {            $this->put_error_in_logfile('Message not posted!');            return FALSE;        }         return TRUE;    } /** public other functions*/     public function print_last_error()    {        if (defined('DEBUG') AND (DEBUG == TRUE))        {                var_dump($self->minicurl->debug_pages());        }                $errors = array_reverse(file(LOG_FILE));        return '<b>Error!</b><br>' . $errors[0];    } /** private auth functions*/     private function need_auth()    {        $result = $this->minicurl->get_file('http://vk.com/settings');        $this->sleep();        return strpos($result, 'HTTP/1.1 302 Found') !==FALSE;    }     private function auth()    {        $this->minicurl->clear_cookies();         $location = $this->get_auth_location();        if($location === FALSE){            $this->put_error_in_logfile('Not recieved Location!');            return FALSE;        }         $sid = $this->get_auth_cookies($location);        if(!$sid){            $this->put_error_in_logfile('Not received cookies!');            return FALSE;        }         $this->minicurl->set_cookies('remixsid=' . $sid . '; path=/; domain=.vk.com');         return TRUE;    }     private function get_auth_location()    {        $html = $this->minicurl->get_file('http://vk.com/');        preg_match('#<input type="hidden" name="ip_h" value="([a-z0-9]*?)" \/>#isU', $html, $matches);         $post = array(            'act' => 'login',            'al_frame' => '1',            'captcha_key' => '',            'captcha_sid' => '',            'email' => $this->email,            'expire' => '',            'from_host' => 'vk.com',            'ip_h' => (isset($matches[1]) ? $matches[1]: ''),            'pass' => $this->pwd,            'q' => '1',        );         $auth = $this->minicurl->get_file('http://login.vk.com/?act=login', $post, 'http://vk.com/');         preg_match('#Location\: ([^\r\n]+)#is', $auth, $match);         $this->sleep();        return ((isset($match[1])) ? $match[1] : FALSE);    }     private function get_auth_cookies($location)    {        $result = $this->minicurl->get_file($location);         $this->sleep();        return ((strpos($result, "setCookieEx('sid', ") === FALSE) ? FALSE :                substr($result, strpos($result, "setCookieEx('sid', '") + 20, 60));    } /** private posting functions*/function curl($url, $cookie, $data = false){$ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);curl_setopt ($ch, CURLOPT_COOKIE, $cookie);curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1');if ($data){curl_setopt($ch, CURLOPT_POSTFIELDS, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);}$response = curl_exec ($ch);curl_close($ch);return $response;}     private function post_to_wall_query($hash, $to_id, $message, $official=FALSE, $friends_only=FALSE, $type='all')    {         $official = $official ? '1' : '';        $friends_only = $friends_only ? '1' : '';         $post = array(            'act' => 'post',            'al' => '1',            'facebook_export' => '',            'friends_only' => $friends_only,            'hash' => $hash,            'message' => $message,            'note_title' => '',            'official' => $official,            'status_export' => '',            'to_id' => $to_id,            'type' => $type,            'attach1_type' => '[b]photo[/b]',                        'attach1' => '[b]141900552_287548118[/b]'        );         $result = $this->minicurl->get_file('http://vk.com/al_wall.php', $post);         $this->sleep();        preg_match('#>\d<!>\d+<!>([\d]+)<!>#isU', $result, $match);         return (isset($match[1]) AND ($match[1] == '0'));    }     private function get_hash($page_id)    {        $result = $this->minicurl->get_file('http://vk.com/' . $page_id);        $this->sleep();         preg_match('#Location\: ([^\r\n]+)#is', $result, $match);        if (isset($match[1]) AND !empty($match[1]))        {            $result = $this->minicurl->get_file('http://vk.com' . $match[1]);            $this->sleep();            unset($match);             preg_match("#act: '([^']+)', code: ge\('code'\)\.value, to: '([^']+)', al_page: '([^']*)', hash: '([^']+)'#i", $result, $match);             $post = array(                'act' => $match[1],                'al' => '1', // хз что это                'al_page' => $match[3],                'code' => $this->phone,                'hash' => $match[4],                'to' => $match[2]            );             $result = $this->minicurl->get_file('http://vk.com/login.php', $post);            $this->sleep();            unset($match);             preg_match('#>/([a-z0-9\.\-_]+)<#is', $result, $match);             if (isset($match[1]) AND !empty($match[1]))            {                $result = $this->minicurl->get_file('http://vk.com/' . $match[1]);                $this->sleep();                unset($match);            }        }        preg_match('#"post_hash":"([^"]+)"#isU', $result, $match);         if (strpos($result, 'action="https://login.vk.com/?act=login'))        {            unset($match[1]);        }         return (isset($match[1]) ? $match[1] : '');    } /** private other functions*/     private function sleep()    {        if ($this->sleeptime)        {            sleep($this->sleeptime + rand(1, 4));        }    }     private function put_error_in_logfile($msg)    {        $msg = '[' . date('Y.m.d H:i:s') . ']: ' . $msg . "\n";        $fp = fopen(LOG_FILE, 'a');        fwrite($fp, $msg);        fclose($fp);    }} ?>
Этот класс рабочий, постит картинку из альбома текст или ссылку. Нужно загрузить фото без использования api как из первого поста. Но работать не хочет :cry:
Аватара пользователя
Александр
Создатель сайта
Создатель сайта
Сообщения: 4574
Зарегистрирован: 27 сен 2009, 16:45

Re: VK api class загрузка фото и пост на стену

Сообщение Александр »

Slyfoxleo
Этот класс требует email и пароль к аккаунту. ВКонтакте запрещают прямое обращение к скриптам. Такие способы ненадежные.

http://flapps.ru/forum/topic1564.html
Slyfoxleo
Сообщения: 3
Зарегистрирован: 16 ноя 2012, 01:55

Re: VK api class загрузка фото и пост на стену

Сообщение Slyfoxleo »

Александр писал(а):Slyfoxleo
Этот класс требует email и пароль к аккаунту. ВКонтакте запрещают прямое обращение к скриптам. Такие способы ненадежные.

http://flapps.ru/forum/topic1564.html
Как обойти. У меня получилось без апи, залить картинку на сервер. У меня сейчас другой вопрос, а именно как получить значение attach1' => '141900552_292899869'
Первые символы понятно, это ID пользователя, а вот откуда берутся остальные. Сниферю запросы, и никак ненайду откуда это приходит.
Аватара пользователя
Александр
Создатель сайта
Создатель сайта
Сообщения: 4574
Зарегистрирован: 27 сен 2009, 16:45

Re: VK api class загрузка фото и пост на стену

Сообщение Александр »

Slyfoxleo
ID пользователя _ ID фотографии
Ответить