1

We are working on a WordPress plugin and we require an API call. We are using the api: http://www.geoplugin.net/json.gp?ip=8.8.8.8

In this context:

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

However, all we get is a 403 error saying:

string(162) "
403 Forbidden
nginx 
"

The API call worked fine using this code:

file_get_contents("http://www.geoplugin.net/json.gp?ip={$ip}")

However, we are only allowed to use wp_remote_get() by WordPress.

We have been at this for a while now and we just can't get it fixed or find any solution. We have even tried to give it an user-agent argument to make it look like a browser connected but even that had no success.

Does anyone know a solution to this?

Thank you for taking your time!

Edit:

An example expected return:

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

Why do you have curly brackets in there?

Maybe sanitize/process the query string with htmlspecialchars() first. The error is complaining about non-standard characters...which is what your curly brackets are doing.

Build the query string with the $ip value, convert with htmlspecialchars(), then add the URL to the query string. Pass that variable to the wp_remote_get() function.

  • It's just out of habit and good practice since it's required in cases of objects or arrays. But even a hard-coded string like the one I gave in the answer http://www.geoplugin.net/json.gp?ip=8.8.8.8 for example gives the same 403 forbidden error. :( – Rick Standaert Jun 29 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.