m7v
5/15/2014 - 8:30 AM

Post to wall VK

Post to wall VK

<?php 
class vkClass {

  public function __construct($email, $passwd, $id) {
    $this->email = $email;
    $this->passwd = $passwd;
    $this->id = $id;
    //$this->profile = $profile;      

    $this->user_agent = $_SERVER['HTTP_USER_AGENT'];

    if (isset($this->email) && isset($this->passwd)) {
      // autenticate the user
      if (!$this->authenticate()) {
        throw new Exception('Failed to authenticate, please check your email and password.');
      }
    }
  }

  public function postImage($url, $text) {
    $postdata = array(
      'act' => 'parse_share',
      'from_host' => 'vk.com',
      'mid' => $this->id,
      'hash' => $this->hash,
      'rhash' => $this->rhash,
      'url' => $url
    );
    $response = $this->post_to("http://cs6036.vk.com/upload.php", $postdata);

    $postdata = array(
      'act' => 'save_draft',
      'al' => '1',
      'hash' => $this->post_hash,
      'media1' => 'share|',
      'msg' => $url
    );

    $response = $this->post_to("http://vk.com/al_wall.php", $postdata);

    $postdata = array(
      'act' => 'a_photo',
      'url' => $url,
      'index' => '1',
      'image' => $url,
      'extra' => '0'
    );

    $response = $this->post_to("http://vk.com/share.php", $postdata);

    $sPos = strpos($response, 'photo_id') + 10;
    $ePos = strpos($response, '}', $sPos);
    $this->imgID = substr($response, $sPos, $ePos - $sPos);

    $postdata = array(
      'act' => 'post',
      'al' => '1',
      'attach1' => $this->id . '_' . $this->imgID,
      'attach1_type' => 'share',
      'description' => '',
      'extra' => '',
      'extra_data' => '',
      'facebook_export' => '',
      'fixed' => '',
      'friends_only' => '',
      'from' => '',
      'hash' => $this->post_hash,
      'message' => '',
      'note_title' => '',
      'official' => '',
      'photo_url' => '',
      'signed' => '',
      'status_export' => '',
      'title' => $text,
      'to_id' => $this->id,
      'type' => '',
      'url' => $url
    );

    $response = $this->post_to("http://vk.com/al_wall.php", $postdata);
  }

  public function postText($message) {
    $postdata = array(
      'act' => 'post',
      'al' => '1',
      'facebook_export' => '',
      'friends_only' => '',
      'from' => '',
      'hash' => $this->post_hash,
      'message' => $message,
      'note_title' => '',
      'official' => '',
      'signed' => '',
      'status_export' => '',
      'to_id' => $this->id,
      'type' => 'all'
    );

    $response = $this->post_to("http://vk.com/al_wall.php", $postdata);

    if ($response) {
      //echo $response;
      //var_dump($response);
      return TRUE;
    }
    echo 'ERROR';
    return FALSE;
  }

  private function authenticate() {
    $postdata = array();
    $response = $this->post_to("http://vk.com/id" . $this->id, $postdata);

    $sPos = strpos($response, 'ip_h') + 7;
    $ePos = strpos($response, "',", $sPos);
    $this->ip_h = substr($response, $sPos, $ePos - $sPos);

    $postdata = array(
      'act' => 'login',
      'success_url' => '',
      'fail_url' => '',
      'try_to_login' => '1',
      'to' => '',
      'vk' => '1',
      'al_test' => '3',
      'from_host' => 'vk.com',
      'ffrom_protocol' => 'http',
      'ip_h' => $this->ip_h,
      'email' => $this->email,
      'pass' => $this->passwd,
      'expire' => ''
    );
    $response = $this->post_to("https://login.vk.com/", $postdata);

    $postdata = array();
    $response = $this->post_to("http://vk.com/id$this->id", $postdata);

    $sPos = strpos($response, 'post_hash') + 12;
    $ePos = strpos($response, 'media_types', $sPos) - 3;
    $this->post_hash = substr($response, $sPos, $ePos - $sPos);

    $sPos = strpos($response, 'upload.php","hash":"') + 20;
    $ePos = strpos($response, '",', $sPos);
    $this->hash = substr($response, $sPos, $ePos - $sPos);

    $sPos = strpos($response, '","rhash":"') + 11;
    $ePos = strpos($response, '"},', $sPos);
    $this->rhash = substr($response, $sPos, $ePos - $sPos);

    if ($response) {
      //echo $response;
      //var_dump($response);
      return TRUE;
    }

    return FALSE;
  }

  private function post_to($url, $data = array()) {
    //check that the url is provided
    if (!isset($url)) {
      return FALSE;
    }

    //send the data by curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookieVK.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookieVK.txt');
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);

    if (count($data) > 0) {
      //POST METHOD
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
  }
}