Advertisement
Guest User

Untitled

a guest
May 5th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Post;
  4.  
  5. class Repost
  6. {
  7.     private $db;
  8.  
  9.     private $db_host = "localhost";
  10.     private $db_name = "";
  11.     private $db_user = "";
  12.     private $db_pass = "";
  13.  
  14.     public function __construct()
  15.     {
  16.         $this->connectDb($this->db_name, $this->db_user, $this->db_pass, $this->db_host);
  17.     }
  18.  
  19.     public function __destruct()
  20.     {
  21.         $this->db = null;
  22.     }
  23.    
  24.     public function addPost($postinfo ,$url, $post) {
  25.             $currentTime = new DateTime("now");
  26.             $query = "INSERT INTO `reposts` (id, url, repost, date) VALUES (:id, :url, :repost, :date)";
  27.             try {
  28.                 $this->db->beginTransaction();
  29.                 $result = $sth->execute(
  30.                 array(
  31.                     ':id' => $postinfo[0],
  32.                     ':url' => $url,
  33.                     ':repost' => $post,
  34.                     ':date' => $currentTime->getTimestamp(),
  35.                 )
  36.             );
  37.                 $this->db->commit();
  38.                 } catch (\PDOException $e) {
  39.                 $this->db->rollback();
  40.                 echo "Database error: " . $e->getMessage();
  41.                 die();
  42.             }
  43.            
  44.             if (!$result) {
  45.                 $info = $sth->errorInfo();
  46.                 printf("Database error %d %s", $info[1], $info[2]);
  47.                 die();
  48.             }
  49.            
  50.             return $result;
  51.         }
  52.        
  53.         public function post($url) {
  54.             $wall = file_get_contents("https://api.vk.com/method/wall.repost?v=5.4&object=wall-" . $url . "&access_token=");
  55.             $wall = json_decode($wall);
  56.             $this = $wall->response;
  57.             return $this;
  58.         }
  59.  
  60.     public function connectdb($db_name, $db_user, $db_pass, $db_host = "localhost")
  61.     {
  62.         try {
  63.             $this->db = new \pdo("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  64.         } catch (\pdoexception $e) {
  65.             echo "database error: " . $e->getmessage();
  66.             die();
  67.         }
  68.         $this->db->query('set names utf8');
  69.  
  70.         return $this;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement