简体   繁体   中英

How to use Apility.io API key?

I'm new in all the php api stuff, so i have started a project with api's but whenever i use my api, it gives me an output of 'bool(false)', i dont know why, some help would be appreciated!! Sorry if my question is too lame, and if possible please tell me where should i use the api key in the url!!

<?php
  include 'includes/header.php';
  $ip = $_SERVER['REMOTE_ADDR'];
  $ip_reputation = file_get_contents("https://api.apility.net/v2.0/ip/".$ip."");
  var_dump($ip_reputation);
?>
<?php 
  include 'includes/footer.php';
?>

You need to authenticate yourself on the API. I'll grant you that, their doc isn't exactly clear on this, you can only see this in the examples.

This, however, means that your code will be a tiny bit longer.

<?php
include 'includes/header.php'

$api_key = 'ReplaceThisWithYourAPICode';

$ip = $_SERVER['REMOTE_ADDR'];
// Set our headers
$opts = [
    "http" => [
        "method" => "GET",
        "header" => "X-Auth-Token: $api_key"
    ]
];

$context = stream_context_create($opts);
$file = file_get_contents("https://api.apility.net/v2.0/ip/".$ip, false, $context);
var_dump($file);

The additional code provides file_get_contents with a stream context containing request headers and the HTTP verb to use (here, GET).

Failure to do so puts you on their "anonymous" plan, whatever that means; I'm relatively certain you're not getting a response body right now, which explains why file_get_contents returns false (as that is an error).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM