V
V
Valery Zinchenko2016-06-22 23:05:31
PHP
Valery Zinchenko, 2016-06-22 23:05:31

How to work with api vk in php?

Help me deal with api vk likes.getlist

$request_params = array(
'type' => 'post',
'owner_id' => '-42621674',
'item_id' => '2146',
'filter' => 'likes',
'skip_own' => '0'
);

$get_params = http_build_query($request_params);
$result = json_decode(file_get_contents('https://api.vk.com/method/likes.getList?'. $get_params));
echo($result -> response[0] -> likes);

I tried 30 different options once, but still the count of reposts (or likes) is simply not displayed. What to do?
Here is the test: tests.hopeness.ml/test1.php

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
littleguga, 2016-06-24
@officialmuse

Your problem is not with the VK API, but with "echo($result -> response[0] -> likes);"
You are accessing an object like an array (see how the json_decode function works).
Decide how to work with $result.
1. Make it an array, then: (do print_r($result) and understand its structure)

$result = json_decode(file_get_contents('https://api.vk.com/method/likes.getList?'. $get_params), true);
echo $result["response"]["count"];

2. If you want to work like with an object: (make var_dump($result) and understand its structure)
$result = json_decode(file_get_contents('https://api.vk.com/method/likes.getList?'. $get_params));
echo($result -> response -> count);

3. Take a closer look at the json_decode function arguments.
Use print_r/var_dump for debugging and full output of variables to understand what's inside them.
Look at the logs there it would be written what exactly the error is.
If the hosting does not have access to the logs, run the script locally, in the console, for example. (for windows you can install git bash + php interpreter and add it to PATH)

D
Dmitry Andronov, 2016-06-23
@wrun

What about the correct IDs?
Just a test:
https://api.vk.com/method/likes.getList?type=post&...
This is this post:
https://new.vk.com/warm_music?w=wall-26797336_54325
Maybe your some closed one?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question