I work with Yandex API.

I get the answer in JSON format.

There are no problems with JS:

$.ajax({ url: "https://api-metrika.yandex.ru/stat/sources/phrases.json", data: { id: "значение id", pretty: "1", oauth_token: "Значение ключа" }, dataType: "jsonp", success: function(data) { var str = ""; var len = data.data.length for (var i = 0; i < len; i++) { str += data.data[i].visit_time + ", "; } $("div").html(str); } }); 

But for security reasons (I don’t want to shine id and token ), I want to get the file through curl , and then just send the result in response to AJAX and work as I worked above in the result of AJAX. That is, parse all js

  • one
    Well, just make a request to the controller for php, which will make a request to the metric and return the file to you. - Moonvvell
  • Something with this and having problems. What nonsense is obtained with curl from me. You can roughly show in which direction to "dig" - zkolya
  • Tell me, what kind of security holes do you patch up with this solution? - Kirill Korushkin
  • so you can view the token and id - zkolya

1 answer 1

php

 if( $curl = curl_init() ) { curl_setopt($curl, CURLOPT_URL, "https://api-metrika.yandex.ru/stat/sources/phrases.json"); curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); curl_setopt($curl, CURLOPT_POST, true); // данные, которые отправляются curl_setopt($curl, CURLOPT_POSTFIELDS, "id=test_id&pretty=1&oauth_token=test_oauth_token"); $out = curl_exec($curl); echo $out; curl_close($curl); } 

js

 $.ajax({ url:"metrika.php", dataType: "jsonp", success: function (data) { console.log(data) }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status); console.log(thrownError); } }); 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin