<?php

function ip_info($ip_info) {
	$url = "https://api.ipdata.co/".$ip_info."?api-key=d804ba93b4614923a2c0ca8cf5a03aed7587e99d3c935c09aa585608";
	return get_request($url);
}


// Sticky API only uses POST requests and uses basic authentication
function post_request(String $url, Array $fields = array()) {

    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_POSTFIELDS => json_encode($fields),
				CURLOPT_CONNECTTIMEOUT => 1,
				CURLOPT_TIMEOUT => 2,
				CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Accept: application/json'
        ),
        CURLOPT_USERNAME => "",
        CURLOPT_PASSWORD => ""
    ]);

    $resp = curl_exec($curl);
    //$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

    return $resp;
}

function get_request(String $url) {

    $curl = curl_init();

    curl_setopt_array($curl, [
		CURLOPT_CONNECTTIMEOUT => 1,
		CURLOPT_TIMEOUT => 2,
		CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Accept: application/json'
        )
    ]);

    $resp = curl_exec($curl);
    //$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

    return $resp;
}