-4

How to extract key values pair from response of stackoverflow API using php?

keys are name and count

link to get response is given below

Link of stackoverflow request api

response is as

{"items":[{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":835623,"name":"java"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":826692,"name":"javascript"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":780233,"name":"c#"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":733762,"name":"php"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":656491,"name":"android"},{"has_synonyms":false,"is_moderator_only":false,"is_required":false,"count":600666,"name":"jquery"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":411780,"name":"python"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":402876,"name":"html"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":361469,"name":"c++"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":324702,"name":"ios"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":314826,"name":"mysql"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":296359,"name":"css"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":271375,"name":"sql"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":253038,"name":"asp.net"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":224602,"name":"objective-c"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":207451,"name":".net"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":202666,"name":"iphone"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":200238,"name":"ruby-on-rails"},{"has_synonyms":false,"is_moderator_only":false,"is_required":false,"count":176868,"name":"c"},{"has_synonyms":false,"is_moderator_only":false,"is_required":false,"count":130813,"name":"ruby"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":129764,"name":"arrays"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":127562,"name":"sql-server"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":116079,"name":"ajax"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":114163,"name":"regex"},{"has_synonyms":false,"is_moderator_only":false,"is_required":false,"count":112127,"name":"xml"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":111066,"name":"json"},{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":107333,"name":"asp.net-mvc"},{"has_synonyms":false,"is_moderator_only":false,"is_required":false,"count":99944,"name":"wpf"},{"has_synonyms":false,"is_moderator_only":false,"is_required":false,"count":98195,"name":"linux"},{"has_synonyms":false,"is_moderator_only":false,"is_required":false,"count":93114,"name":"django"}],"has_more":true,"quota_max":300,"quota_remaining":228}

I have done this but getting no response in php

<?php

$url = 'https://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));

$result = curl_exec($cURL);

curl_close($cURL);



print_r($result);

?>

I have also done this using javascript there I'm getting response but there is no way that javascript can interact with database.I have tried sending it to php file from there but I'm unable to get key value pair from request.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
    type:"GET",
    url:"https://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow",
    success:function(data){

        $.ajax({
                type:"POST",
                url:"savetodb.php",
                data:"data="+JSON.stringify(data),
                success:function(daa){
                alert(daa)  }
                })
    }
})
</script>

I have also tried php function file_get_contents([URL])

code is as follow

<?php
$url = "https://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
print_r($json_data);

?>

I'm getting error

1.file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known

2.file_get_contents(https://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known

  • What is supposed to be the key? What programming language? Please be more specific. – Dunken Apr 16 '15 at 08:30
  • 1
    Based on the new info, it seems that you have a problem with the DNS resolution on your web server. The javascript version is run from your computer, and it works, but from PHP (so from the server), it's not working. I suggest you move to chat (http://chat.stackoverflow.com/rooms/11/php). Also, if you're using Nginx, you should check if you have it properly configured (http://serverfault.com/questions/612992/nginx-and-php-cant-resolve-hostname-and-make-connection-with-fqdn) as it is not using the systerm's resolver. – Razvan Stefanescu Apr 16 '15 at 13:48
  • I'm running it using wamp on my local system. – HimanshuArora9419 Apr 16 '15 at 13:58
  • two ideas: 1. check your php.ini and make sure these attributes are enabled allow_url_fopen = On allow_url_include = On 2. Create a phpinfo() file [ phpinfofile.com just in case ] and check if these extensions are enabled OpenSSL Socket Especially for windows user running xammp or wammp by disabling PHP_OpenSSL extension you might be unable to connect through @fsockopen; both ideas are from http://serverfault.com/a/613174/93764 – Razvan Stefanescu Apr 16 '15 at 14:33
  • I checked both allow_url_include was off I turn it on but results is same, no response. – HimanshuArora9419 Apr 16 '15 at 16:46

1 Answers1

0

As stated in the Stack Exchange API documentation,

All API responses are JSON

so you should search for JSON parsing in the programming language that you need to use.

For the DNS part I think you should check this answer on serverfault; focus on first two things:

[X]

First thing to check is to check your php.ini and make sure these attributes are enabled

allow_url_fopen = On

allow_url_include = On

[X]

Create a phpinfo() file [ phpinfofile.com just in case ] and check if these extensions are enabled

OpenSSL

Socket

Especially for Windows users running xammp or wammp by disabling PHP_OpenSSL extension you might be unable to connect through @fsockopen

Community
  • 1
  • 1
  • I have tried but in php it shows error while fetching content file_get_contents(http://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow): failed to open stream: php_network_getaddresses: getaddrinfo failed: – HimanshuArora9419 Apr 16 '15 at 08:50
  • Based on your error there you might have a DNS problem (http://stackoverflow.com/questions/18885496/php-network-getaddresses-getaddrinfo-failed) – Razvan Stefanescu Apr 16 '15 at 11:28
  • If I use ajax than I'm getting response. I have attached javascript code to in question but problem is that javascript can't interact with mysql. – HimanshuArora9419 Apr 16 '15 at 11:32