API tutorial.
Posted: Sun Jun 19, 2011 8:19 am
I have followed your youtube tutorial for the developers API.
I am getting a slightly disturbing issue. Data is not always being fed back to me.
it can take 5-6 refreshes of my page before any information is displayed. Now for what i need this API for, this is an issue i need resolved.
Here are my 3 scripts used for the API.
api_interface
it manages to log every hit, just doesnt return data all the time.
I am getting a slightly disturbing issue. Data is not always being fed back to me.
it can take 5-6 refreshes of my page before any information is displayed. Now for what i need this API for, this is an issue i need resolved.
Here are my 3 scripts used for the API.
api_interface
class taptin { const API_URL = 'http://www.website.net/api/api.php'; // internal function for sending post data private static function send_post_data($data) { $url = parse_url(self::API_URL); $boundary = md5(microtime(true)); $post = ''; foreach($data as $name => $value) { $post .= "--{$boundary}\r\n"; $post .= "Content-Disposition: form-data; name=\"{$name}\"\r\n\r\n"; $post .= "{$value}\r\n"; } $post .= "--{$boundary}--\r\n"; if(isset($url['query'])) { $head = "POST {$url['path']}?{$url['query']} HTTP/1.1\r\n"; } else { $head = "POST {$url['path']} HTTP/1.1\r\n"; } $head .= "Host: {$url['host']}\r\n"; $head .= "Content-Type: multipart/form-data; boundary=\"{$boundary}\"\r\n"; $head .= "Content-Length: " . strlen($post) . "\r\n"; $head .= "Connection: close \r\n\r\n"; $socket = fsockopen($url['host'], ((isset($url['port'])) ? $url['port'] : 80)); fwrite($socket, "{$head}{$post}"); return (end(explode("\r\n\r\n", stream_get_contents($socket)))); } // Gets the user_id for the reference id given public static function user_by_ref($ref) { return json_decode(self::send_post_data(array('log_user' => $ref)), true); } }api.php
include ("api.inc.php"); header('Content-Type: application/json'); if(isset($_POST['log_user'])) { $errors = array(); $user = (empty($errors)) ? get_user_ref($_POST['log_user']) : false; echo json_encode(array( 'user_id' => $user['user_id'], 'username' => $user['username'], 'gender' => $user['gender'], 'errors' => $errors, )); } else { echo json_encode(array( 'get_user_info' => 'returns the basic information of a specified user.', 'log_user' => 'returns the user id of a user logging onto your game.', )); } log_api_request($_SERVER['REMOTE_ADDR']);and api.inc.php
$db_host = "xxxxxxx"; $db_user = "xxxxxxxxx"; $db_pass = "xxxxxxxx"; $db_name = "xxxxxxxx"; $dba = new mysqli($db_host, $db_user, $db_pass, $db_name); // Logs a new request received from the given IP address function log_api_request($ip) { global $dba; $ip = $dba->real_escape_string($ip); $query = "INSERT INTO `api_log` (`ip`, `date`, `requests`) VALUES (INET_ATON('{$ip}'), NOW(), 0) ON DUPLICATE KEY UPDATE `requests` = `requests` + 1"; $dba->query($query); } // Gets the total number of requests made for today function get_day_request($ip) { $ip = mysqli_real_escape_string($ip); $query = "SELECT `requests` FROM `api_log` WHERE `ip` = INET_ATON('{$ip}') AND `date` = DATE(NOW())"; $sql = $dba->query($query); return ($sql->num_rows == 1) ? $sql->fetch_row(0) : 0; } function get_user_ref($ref) { global $dba; $ref = preg_replace('#[^A-Za-z0-9]#', '', $ref); $query = "SELECT user_id, username, gender FROM table WHERE porting_ref='$ref' LIMIT 1"; $sql = $dba->query($query); if($sql->num_rows > 0) return $sql->fetch_assoc(); }am i missing something? or is this just a risk using this kind of API?
it manages to log every hit, just doesnt return data all the time.