A
A
Anton Titov2018-05-14 07:51:31
PHP
Anton Titov, 2018-05-14 07:51:31

What is wrong with php script for VK API?

A script was written for posting records to open VK communities using the wall.post method. The script runs successfully and does not give any errors, but no new records appear in the group specified in the script. What could be the problem?
PS. A token without time limits was received and entered into the script. Token for stand-alone application.
Script:

<?php
/*
VK-API WALL.POST Отправляем запись в группу методом post, через CURL
*/
$group_id ="-123108209";
$token = "тут мой токен";
$api_ver = "5.74";
$text = file_get_contents('post_data.txt');
$url = sprintf('https://api.vk.com/method/wall.post?');
$ch = curl_init();
curl_setopt_array( $ch, array (
    CURLOPT_POST           => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_SSL_VERIFYHOST => FALSE,
    CURLOPT_POSTFIELDS     => array(
        "owner_id"     => $group_id,
        "from_group"   => 1,
        "Message"      => $text,
        "access_token" => $token,
        "v"            => $api_ver,

),
    CURLOPT_URL => $url,
));
$query = curl_exec($ch);
curl_close($ch);
if(!$query){
    printf('Error');
    exit;
}
else{
    printf('Success');
    exit;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2018-05-14
@fubarblg

Parse the answer from VK , it is quite possible that it contains a description of the error .
Instead of

printf('Success');
exit;

need something like
$decoded = json_decode($query);
if(!$decoded) {
  printf("ВК вернул невообразимую фигню: %s", $query);
}
else if( $decoded->error) {
  printf("Ошибка %d: %s\n", $decoded->error->error_code, $decoded->error->error_msg);
} else if( $decoded->response) {
  printf("Пост опубликован с id %s\n", $decoded->response);
}
exit();

Upd. Perhaps the point is in the posted message and curl. Try doing this instead:CURLOPT_POSTFIELDS => array( ...
CURLOPT_POSTFIELDS     =>  http_build_query( array( ... ))

S
Scuba, 2018-05-14
@targetologru

Here is my working code:

function vk ($vkscript) {
  $token = '7afa4204ca81dccc0549e5ee3**********80f9ac76********a1de69d3f4a9d933';
  $url = 'https://api.vk.com/method/execute';
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, array('access_token' => $token, 'code' => $vkscript, 'v' => '5.69'));
  $response = curl_exec($ch);
  curl_close($ch);
  return json_decode($response, true);
}

$vkscript = '
var req_params = {
  "owner_id" : "-160******",
  "from_group" : "1",
  "message" : "'.$for_pub['text'].'",
  "attachments" : "photo'.$result['response'][0]['owner_id'].'_'.$result['response'][0]['id'].'",
  "publish_date" : "'.$publish_date.'",
  "v" : "5.69"
};
var result = API.wall.post(req_params);
return result;';

print vk($vkscript );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question