API VKontakte. Requests to API and getting data

Home » Tutorials » JavaScript » API VKontakte. Requests to API and getting data
In last lesson we began to consider API VKontakte. In this lesson we’ll make our first requests to api methods and get data about vkontakte user.
We’ll make request with several ways – javascript, jquery ajax and php.
You may read about api requests in details with link. If you make request with javascript, you must make it with jsonp. This is due to the fact that request between different domains are forbidden by a lot of browsers for security. Such requests сharacterized by callback functions.

Code lesson

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>API Вконтакте</title>
</head>
<body>


	<h1>API Вконтакте</h1>

	<script type="text/javascript">
		var script = document.createElement('SCRIPT');
		script.src = "https://api.vk.com/method/users.get?user_ids=kamabzalov&fields=bdate&v=5.67&callback=callbackFunc";
		document.getElementsByTagName("head")[0].appendChild(script);
		function callbackFunc(result) {
			console.log(result.response[0].first_name);
		}
	</script>


	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

	<script>
		$.ajax({
			url: 'https://api.vk.com/method/users.get?user_ids=kamabzalov',
			type: 'GET',
			dataType: 'jsonp',
		})
		.done(function(data) {
			console.log(data.response[0].last_name);
		});
	</script>


	<?php

		$res = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids=kamabzalov"), true);

		echo $res['response'][0]['uid'];


	?>


</body>
</html>

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This