K
K
Kirill2014-10-11 16:37:10
JSON
Kirill, 2014-10-11 16:37:10

How to get data from VK API using wall.get method?

Hey!
Guys, I created a request, I want to use JSONP, because I am writing a jQueryMobile application.
The request looks like

https://api.vk.com/method/wall.get?v=5.25&filter=owner&domain=' + domain + '&count=' + count + '&callback=GetWallVK

In the function, I indicated at the moment just for the test, the output of the received data to the console. Everything is fine here, the object is coming.
But the question is the following, maybe someone will tell you more clearly, because. I'm just learning how to get data out of there correctly?
If someone wrote a script for themselves, please share.
I found one script that parses wall data into an RSS feed, but it's in PHP. I am posting this piece of code below.
$url = "http://api.vk.com/method/wall.get?owner_id=$owner_id&count=90";
$response = file_get_contents($url);
$wall = json_decode($response);
for ($i = 1; $i<=count($wall->response)-1; $i++) {
    $wall->response[$i]->text = preg_replace("#&mdash;#", '', $wall->response[$i]->text);
    $wall->response[$i]->text = html_entity_decode($wall->response[$i]->text, null, 'utf-8');
    $newItem = $feed->createNewItem();
    $title = explode('<br>',$wall->response[$i]->text);
    $title = $title[0];
    $title = (mb_strlen($title, 'utf-8')<=100) ? $title : mb_substr($title,0,100,'utf-8').'...';
    //echo $wall->response[$i]->id."\t".$title."\t".$wall->response[$i]->text."\n";
    $newItem->setTitle($title);
    $newItem->setLink("http://vk.com/wall{$owner_id}_{$wall->response[$i]->id}");
    $newItem->setDate($wall->response[$i]->date);
    $description = $wall->response[$i]->text;
    
    if (isset($wall->response[$i]->attachments)) {
        foreach ($wall->response[$i]->attachments as $attachment) {
            switch ($attachment->type) {
                case 'photo': {
                    $description .= "<br><img src='{$attachment->photo->src_big}'/>";
                    break;
                }
                case 'audio': {
                    $description .= "<br><a href='http://vk.com/wall{$owner_id}_{$wall->response[$i]->id}'>{$attachment->audio->performer} &ndash; {$attachment->audio->title}</a>";
                    break;    
                }
                case 'doc': {
                    $description .= "<br><a href='{$attachment->doc->url}'>{$attachment->doc->title}</a>";
                    break;
                }
                case 'link': {
                    $description .= "<br><a href='{$attachment->link->url}'>{$attachment->link->title}</a>";
                    break;
                }
                case 'video': {
                    $description .= "<br><a href='http://vk.com/video{$attachment->video->owner_id}_{$attachment->video->vid}'><img src='{$attachment->video->image_big}'/></a>";
                    break;
                }
            }
        }
    }
    
    $newItem->setDescription($description);
    $newItem->addElement('guid', $wall->response[$i]->id);
    $feed->addItem($newItem);
}

I just can’t even understand how I can run such a thing on JS, because I looked into the object and there, in each object, different data is sometimes stored - where is the photo, where is the text, where is the video - but the biggest snag is that there are reposts on the wall and the data is already stored there in a different way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksandr R, 2016-09-30
@alexhouse

Everything is simple there:
I sent a request, received a response, the response has a clear structured form in JSON. It must be parsed into components by levels, and then parse the data from the necessary variables.
Here is a simple example of how to parse a response in python -

response = vk.wall.get(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0)
            ts = str(response['items'][0]['date'])
            print(ts)

In this case, response gets all the response data from vk, then ts pulls out only the necessary variables from the entire response.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question