使用 php 从 stackoverflow 的 api 响应中提取键值对以将它们保存在 mysql 中

extract key values pair from api response of stackoverflow to save them in mysql using php

如何使用 php 从 Whosebug API 的响应中提取键值对?

键是姓名和人数

link 得到响应如下

Link of Whosebug request api

响应如

{"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}

我已经这样做了,但在 php

中没有得到任何回应
<?php

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

$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);

?>

我也使用 javascript 完成了此操作,我收到了回复,但 javascript 无法与 database.I 进行交互已尝试将其发送到 php 文件,但我无法从请求中获取键值对。

<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=Whosebug",
    success:function(data){

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

我也试过 php 函数 file_get_contents([URL])

代码如下

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

?>

我遇到错误

1.file_get_contents(): php_network_getaddresses: getaddrinfo 失败: 不知道这样的主机

2.file_get_contents(https://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=Whosebug): 无法打开流: php_network_getaddresses: getaddrinfo 失败: 没有这样的主机是已知的

Stack Exchange API documentation

中所述

All API responses are JSON

所以你应该搜索 JSON 解析你需要使用的编程语言。

对于 DNS 部分,我认为您应该检查一下 this answer on serverfault;关注前两件事:

[X]

首先要检查的是检查您的 php.ini 并确保这些属性已启用

allow_url_fopen = On

allow_url_include = On

[X]

创建一个 phpinfo() 文件 [phpinfofile.com 以防万一] 并检查是否启用了这些扩展

OpenSSL

Socket

特别是 Windows 用户 运行 xammp 或 wammp 通过禁用 PHP_OpenSSL 扩展你可能无法通过@fsockopen

连接