A
A
Alex2016-02-09 22:42:13
PHP
Alex, 2016-02-09 22:42:13

How to use VK API using php?

Banal example:

$params = array(
      'user_ids' => 1,
    );
    $url = 'https://api.vk.com/method/users.get'.http_build_query($params);
    $res = file_get_contents($url);

But I get an error:
Warning : file_get_contents( https://api.vk.com/method/users.get?user_ids=1): failed to open stream: Connection refused

Answer the question

In order to leave comments, you need to log in

5 answer(s)
L
littleguga, 2016-02-09
@Kozack

Most likely your server ip is banned by VK. This is very possible if you have shared-hosting, not vps. Just check with file_get_contents("vk.com")

S
Sergey Savostin, 2016-02-09
@savostin

https://github.com/bocharsky-bw/vkontakte-php-sdk
https://github.com/mordehaigerman/VKontakte-PHP-SDK

W
Wily, 2017-03-18
@wily

<?php
$params = array(
     'user_ids' => 1,
 );
$url = 'https://api.vk.com/method/users.get?'.http_build_query($params);
$res = file_get_contents($url);

E
entermix, 2016-02-09
@entermix

Try using an unsecured connection, i.e. not https but http

Z
Zakhar Storozhuk, 2016-02-10
@Phell

function getUrl($url, $params) {
  $ch = curl_init();
  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, $params);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36');
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

function getMethod($method, $params) { 
  $url = method_url.$method;
  $params = http_build_query($params)."&lang=".method_lang;
  $response = getUrl($url, $params);
  return json_decode($response, true);
}

Usage:
$params = array('access_token' => "token");
  $result = getMethod("vk.method", $params);

vk.method - the method you are using.
cURL via Post which guarantees sending multi-line requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question