S
S
Sergey Zhuzhgov2019-07-16 10:30:53
API
Sergey Zhuzhgov, 2019-07-16 10:30:53

How to fix the automatic sending of a post to a VK group?

There is a php script on the server that sends a post with an attached picture to the VK group feed
Until recently, everything worked fine, but now the picture from the server has stopped uploading.
Here is the code:

require 'class/vk.php';
require 'class/post.php';

$token = ' ... ';
$user_id = null;
$group_id = ' ...132465789... ';

$text = $tempbody;
$image = ' ... /vk/meteoimg.jpg';

try {
    $vk = \vkApi\vk::create($token);
    $post = new \vkApi\post($vk, $user_id, $group_id);
    $post->post($text, $image);
    echo 'Success!';
} catch(Exception $e){
    echo 'Error: <b>' . $e->getMessage() . '</b><br />';
    echo 'in file "' . $e->getFile() . '" on line ' . $e->getLine();
}

and post.php file
namespace vkApi;

class post{
    private $vk;
    private $owner;
    function __construct(vk $vk, $user = null, $group = null){
        $this->vk = $vk;
        if(!$user && !$group){
            throw new \Exception('Not found group or user');
        }
        $this->owner = array(
            'type' => $user ? 'owner_id' : 'group_id',
            'value' => $user ? $user : $group
        );
        $this->owner['value'] = (int)preg_replace('/([^\d]+)/', '', $this->owner['value']);
    }

    function post($text, $img = null){
        if($img) {
            $data = $this->load($img);
            $img = $data->response[0]->id;
        }
        $data = array(
            'message' => $text,
            'owner_id' => $this->owner['value']
        );
        if($img){
            $data['attachments'] = $img;
        }
        if($this->owner['type'] == 'group_id'){
            $data['owner_id'] = '-' . $data['owner_id'];
        }

        $data = $this->vk->get('wall.post', $data);
        if(isset($data->error)){
            throw new \Exception($data->error->error_msg);
        }
        return $data;
    }

    function load($src){
        $photo = (array)$this->getPhoto($src);
        $photo[$this->owner['type']] = $this->owner['value'];
        $data = $this->vk->get('photos.saveWallPhoto', $photo);
        return $data;
    }

    private function getPhoto($src){
        $name = __DIR__ . DIRECTORY_SEPARATOR . '1.png';
        file_put_contents($name, file_get_contents($src));
        $ch = curl_init($this->getServer());
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'photo' => '@' . $name
        ));
        $response = curl_exec( $ch );
        curl_close( $ch );
        return json_decode($response);
    }

    private function getServer(){
        $data = $this->vk->get('photos.getWallUploadServer', array(
            $this->owner['type'] => $this->owner['value'],
        ));
        return $data->response->upload_url;
    }


}

and vk.php
namespace vkApi;

class vk {
    private $token;
    private $count = -1;
    private static $class = null;

    public static function create($token){
        if(!self::$class){
            self::$class = new vk($token);
        }
        return self::$class;
    }

    private function __clone(){}
    private function __construct($token){
        $this->token = $token;
    }

    function get($method, array $data){
        $this->count ++;
        if($this->count >= 3){
            $this->count = 0;
            sleep(1);
        }
        $params = array();
        foreach($data as $name => $val){
            $params[$name] = $val;
      $params['v'] = '5.101';
            $params['access_token'] = $this->token;
        }
        $json = file_get_contents('https://api.vk.com/method/' . $method . '?' . http_build_query($params));
        return json_decode($json);
    }
}

Please tell me how to fix.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
volodec, 2019-07-16
@volodec

Print in the log what post::load and post::getPhoto returns, after that it will become clearer to you what's going on

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question