D
D
Dmitry2016-03-17 18:15:19
PHP
Dmitry, 2016-03-17 18:15:19

How to process the response received after a request to the VK server?

The user enters the group id into the input box and clicks the button. When the button is clicked, the get_group() function is called. It is described below. The AJAX request is sent to the script, which is also shown below. How can I get a response from the server and process it? In what file to catch it?
I thought that this can be done using curl, but something does not work out. I apologize for stupid questions for someone, but I just started working with this library and do not have the necessary experience. And the understanding of the work of curl has not yet come.

function get_group(){
        $.ajax({
            url: "do_request.php",
            method: "GET",
            cache: false,
            data:"request=https://api.vk.com/method/groups.getMembers?group_id="+document.getElementById("group_id").value,
            success:function(data){
                document.getElementById("output_data").innerHTML="Работает";
            },
            error:function(jqXHR,textStatus){
                alert("Ошибка №"+jqXHR.status+": "+textStatus);
            }
        })
    }


The code of the script to which the AJAX request is sent.
<?php
$request=curl_init($_REQUEST["request"]);
curl_setopt($request,CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($request);
echo $result;
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
littleguga, 2016-03-17
@Papa_kfc

1. Your js code sends a request to the php script, the php script does something and gives a response, then a success event occurs in js and the information that the php script gave will be in the variable (data).
2. For this task, you can use not curl, but the banal file_get_contents.
You make a request to the desired url and display the response (it will be in json format => it is more convenient to process it already in js).
3. I would redo the code a little and pass the script to php not a link, but simply the id of the VK group.

<?php
$vk_url = "https://api.vk.com/method/groups.getMembers?group_id=".$_GET["group_id"];
$vk_result = file_get_contents($vk_url);
echo $vk_result;
?>

And the js code is like this: (since you are already using jQuery, you can take everything from it and not use document.getElementById)
function get_group(){
        $.ajax({
            url: "do_request.php",
            method: "GET",
            cache: false,
            data:"group_id=" +$("#group_id").value,
            success:function(data){
                console.log(data); //здесь будет ответ от вашего php скрипта
                $("#output_data").text("Работает");
            },
            error:function(jqXHR,textStatus){
                alert("Ошибка №"+jqXHR.status+": "+textStatus);
            }
        })
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question