用于与Vk.com API交互的库

0.6.0 2016-10-27 08:07 UTC

This package is not auto-updated.

Last update: 2024-09-11 12:14:26 UTC


README

Build Status Code Style Status

该库将帮助您组织与Vk.com API的工作

安装

您可以通过Composer获取库及其所有依赖项

composer require "getjump/vk:*"

说明

好的,这就是我们如何实例化用于将来请求的主要对象的方式

$vk = getjump\Vk\Core::getInstance()->apiVersion('5.5')->setToken(>>> HERE YOUR TOKENS GOES <<<);

您可以在以下位置获取一些令牌:您可以在以下位置获取一些令牌:http://oauth.vk.com/authorize?client_id=3470411&scope=messages,photos,groups,status,wall,offline&redirect_uri=blank.html&display=page&v=5.5&response_type=token我不能偷取它们,因为这是VK方面的东西,范围意味着你需要什么权利,我建议尽可能多,如果你不想有问题。如果你想使用网站授权,请看下一个片段。

$vk = getjump\Vk\Core::getInstance()->apiVersion('5.5');

$auth = getjump\Vk\Auth::getInstance();
$auth->setAppId('3470411')->setScope('SCOPE')->setSecret('SECRET CODE')->setRedirectUri('http://localhost/test.php'); // SETTING ENV
$token=$auth->startCallback(); // Here we will have token, if everything okay

printf("<a href='%s' target='_top'>LINK</a>", $auth->getUrl());
if($token) {
    $vk->setToken($token);
    $vk->request('users.get', ['user_ids' => range(1, 100)])->each(function($i, $v) {
        if($v->last_name == '') return;
        print $v->last_name . '<br>';
    });
}

我已经为您准备了一些包装器,如果您想要更多,请进行pull request,但您仍然可以选择不使用它们,或者使用类似的东西。

$vk->request('friends.get', ['user_id' => '15157875'])->each(function($i, $v) {});

这就是长轮询,它工作得像地狱一样,像您看到的那样快。

// Long pooling loop
$lp = new getjump\Vk\Wrapper\LongPoll($vk);
$lp->doLoop();

我们将做一些很酷的事情,比如亲吻。您可以这样做,它会起作用

//KISS
$user=new getjump\Vk\Wrapper\User(getjump\Vk\Core::getInstance()->apiVersion('5.5'));
$user->get(1, 'photo_max_orig, sex'); //It will contain RequestTransaction, and will wait for your action, like getting response ->response or calling ->each(callback)
//Since __get and __call are overrided, we will request for a data, only when it neeeded

我们可以使用我自己的萨哈林技术,并使用生成器获取该请求的所有VK内容

// Friends gets
$friends = new getjump\Vk\Wrapper\Friends($vk);
foreach($friends->get(15157875, 'first_name, last_name')->batch(100) as $f) //BATCH MEAN $f WILL CONTAIN JUST 100 ELEMENTS, AND REQUEST WILL MADE FOR 100 ELEMENTS
{
    /**
     * @var $f \getjump\Vk\ApiResponse;
     */
 
    $f->response->each(function($i, $j) {
        if(!$j->online) return;
        print $j->getName() . '<br>';
    });
}

但您仍然可以这样做旧式的事情

 //SECOND OPTION TO JUST GET EVERYTHING, WITHOUT count BEING SEND
$friends->get(15157875, 'first_name, last_name')->response->each(function($i, $d) {
     if($d->online)
     {
         print $d->getName() . '<br>';
     }
});

此片段将显示您最后200条消息

//MESSAGES
$data = $vk->request('messages.get', ['count' => 200]);
 
$userMap = [];
$userCache = [];
 
$user = new \getjump\Vk\Wrapper\User($vk);
 
$fetchData = function($id) use($user, &$userMap, &$userCache)
{
    if(!isset($userMap[$id]))
    {
        $userMap[$id] = sizeof($userCache);
        $userCache[] = $user->get($id)->response->get();
    }
 
    return $userCache[$userMap[$id]];
};

//REQUEST WILL ISSUE JUST HERE! SINCE __get overrided
$data->each(function($key, $value) use($fetchData) {
    $user = $fetchData($value->user_id);
    printf("[%s] %s <br>", $user->getName(), $value->body);
    return;
});

再次施展黑魔法。VK有一个名为execute的方法,可以接受类似JS代码的内容。看看我为这个做了什么。

$js1 = $vk->request('messages.get', ['count' => 200, 'offset' =>0 * 200])->toJs(); //IT WILL RETURN VkJs object
$js2 = $vk->request('messages.get', ['count' => 200, 'offset' =>1 * 200])->toJs();
$js3 = $vk->request('messages.get', ['count' => 200, 'offset' =>2 * 200])->toJs();
$js4 = $vk->request('messages.get', ['count' => 200, 'offset' =>3 * 200])->toJs();


$js1
        ->append($js2) // WE ARE APPENDING js2 to js1
        ->append($js3)
        ->append($js4) 
        ->execute() // WE WANT EXECUTE THIS (actually it will return RequestTransaction)
        ->response //AS FOR NOW WE REALLY DO SOME REQUEST TO API 
        ->each(
            function($i, $v) //FIRST CALLBACK IS NEEDED TO GO FOR EVERY PART OF RESPONSE, ARRAY WITH 4-ELS IN OUR CASE
            {
                $v->each(function($c, $d) { // SECOND TO CHECK EVERY ELEMENTS IN ARRAY WITH 200 ELEMENTS
                    if(isset($d->body)) print $d->body; //WE JUST OUTPUTTING MESSAGE IF IT SET
                });
            });