我们正在开发WordPress插件,我们需要一个API调用。我们正在使用api:http://www.geoplugin.net/json.gp?ip=8.8.8.8
在这种情况下:
$response = wp_remote_get("http://www.geoplugin.net/json.gp?ip={$ip}", array(
\'timeout\' => 45,
\'redirection\' => 5,
\'user-agent\' => \'Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)\',
));
$dataArray = json_decode(wp_remote_retrieve_body($response));
var_dump($response);
然而,我们得到的只是一个403错误:
string(162) "
403 Forbidden
nginx
"
使用以下代码,API调用工作正常:
file_get_contents("http://www.geoplugin.net/json.gp?ip={$ip}")
但是,我们只允许使用
wp_remote_get()
通过WordPress。
我们已经在这方面做了一段时间了,但我们无法修复它或找到任何解决方案。我们甚至尝试给它一个用户代理参数,使它看起来像一个连接了的浏览器,但即使这样也没有成功。
有人知道这个问题的解决方案吗?
感谢您抽出时间!
编辑:
预期回报示例:
{
"geoplugin_request":"8.8.8.8",
"geoplugin_status":206,
"geoplugin_delay":"2ms",
"geoplugin_credit":"Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http:\\/\\/www.maxmind.com\'>http:\\/\\/www.maxmind.com<\\/a>.",
"geoplugin_city":"",
"geoplugin_region":"",
"geoplugin_regionCode":"",
"geoplugin_regionName":"",
"geoplugin_areaCode":"",
"geoplugin_dmaCode":"",
"geoplugin_countryCode":"US",
"geoplugin_countryName":"United States",
"geoplugin_inEU":0,
"geoplugin_euVATrate":false,
"geoplugin_continentCode":"NA",
"geoplugin_continentName":"North America",
"geoplugin_latitude":"37.751",
"geoplugin_longitude":"-97.822",
"geoplugin_locationAccuracyRadius":"1000",
"geoplugin_timezone":"",
"geoplugin_currencyCode":"USD",
"geoplugin_currencySymbol":"$",
"geoplugin_currencySymbol_UTF8":"$",
"geoplugin_currencyConverter":1
}