PHP code example of programmis / php-vk-api-sdk

1. Go to this page and download the library: Download programmis/php-vk-api-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

programmis / php-vk-api-sdk example snippets


$server = new PhotosGetUploadServer($token);
$server->setAlbumId($album_id)
    ->setGroupId($group_id);
if (!$server->doRequest()) {
    return;
}
if (!$server->upload([$img_path])) {
    return;
}
$uploadResult = $server->getUploadResult();
$photos = new PhotosSave($token);
$photos->setAlbumId($album_id)
    ->setGroupId($group_id)
    ->setServer($uploadResult->getServer())
    ->setPhotosList($uploadResult->getPhotosList())
    ->setHash($uploadResult->getHash())
    ->setCaption($text);
if (!$photos->doRequest()) {
    return;
}

$users = \VkApi\Vk::users()->get()
    ->addUserId(1)
    ->addField('sex')
    ->execute();
foreach ($users as $uI) {
    echo $uI->getId() . "\n";
    echo $uI->getFirstName() . "\n";
    echo $uI->getLastName() . "\n";
    echo $uI->getSex() . "\n";
}
//OR
$user = new \VkSdk\Users\UsersGet();
$user->setUserIds([1])
    ->addField('sex');
if($user->doRequest()){
    foreach($user->getUsersInfo() as $uI ){
        echo $uI->getId() . "\n";
        echo $uI->getFirstName() . "\n";
        echo $uI->getLastName() . "\n";
        echo $uI->getSex() . "\n";
    }
}

$joinGroup = new GroupsJoin();
$joinGroup->setGroupId(1);
if( $joinGroup->doRequest()){
    echo $joinGroup->getResponse();
}

$wallPost = new WallPost();
$wallPost->setOwnerId(1)
    ->setMessage("test");
if($wallPost->doRequest()){
    echo $wallPost->getPostId();
}

$album = new PhotosCreateAlbum();
$album->setTitle("My new test album");
if($album->doRequest()){
    echo $album->getAlbumInfo()->getId();
}

$friendsAdd = new FriendsAdd();
$friendsAdd->setUserId(1)
    ->setText("Add me please");
if($friendsAdd->doRequest()){
    switch($friendsAdd->getResponse()){
        case FriendsAdd::RESULT_SEND:
        case FriendsAdd::RESULT_ACCEPT:
        case FriendsAdd::RESULT_REPEAT:
            echo "OK";
            break;
    }
}

$statusSet = new StatusSet();
$statusSet->setText("This is my new status");
if($statusSet->doRequest()){
    echo $statusSet->getResponse();
}

$boardComment = new BoardCreateComment();
$boardComment->setMessage("This is my new comment")
    ->setGroupId(1)
    ->setTopicId(21972158);
if ($boardComment->doRequest()) {
    echo $boardComment->getCommentId();
}

$online = new AccountSetOnline();
if($online->doRequest()){
    echo $online->getResponse();
}

$message = new MessagesSend();
$message->setMessage("Hello people")
    ->setUserId(1);
if($message->doRequest()){
    echo $message->getMessageId();
}

$poll = new PollsCreate();
$poll->setOwnerId(1)->setIsAnonymous(1)
     ->setQuestion("Would you like to provide financial assistance for a set of these classes?")
     ->addAnswer('Yes')
     ->addAnswer('Of course');
if($poll->doRequest()){
    echo $poll->getPollInfo()->getId() . "\n";
}

\VkSdk\Includes\Request::setLogger(new \youre\Logger());

$antiCaptcha = new \AntiCaptcha\RuCaptcha(); //integrated in project now
$antiCaptcha->setAccessToken($anti_captcha_access_token);
\VkSdk\Includes\Request::setAntiCaptcha($antiCaptcha);

\VkSdk\Includes\Request::setBeforeRequestCallback($this, 'beforeRequest');
\VkSdk\Includes\Request::setAfterRequestCallback($this, 'afterRequest');
//OR
\VkSdk\Includes\Request::setBeforeRequestCallback(function(){});
\VkSdk\Includes\Request::setAfterRequestCallback(function(){});