API Tutorial... Im Stuck
Posted: Sun Jul 24, 2011 3:34 pm
Hey,
Last night i was working through the API tutorials On Your Youtube Page i recieved no errors when completed everything worked but the purpose of the API seems to be different?? can i use the API to allow External sites to Login with users from my Database using the API, i thought i could so i tried various things Code Below:
api.inc.php
phpkid01
Last night i was working through the API tutorials On Your Youtube Page i recieved no errors when completed everything worked but the purpose of the API seems to be different?? can i use the API to allow External sites to Login with users from my Database using the API, i thought i could so i tried various things Code Below:
api.inc.php
<?php
require("int.php");
//Logs a request.
function log_api_request($ip){
$ip = mysql_real_escape_string($ip);
$sql = "INSERT INTO `api_log` (`ip`, `date`, `requests`) VALUES (INET_ATON('{$ip}'), NOW(), 0)
ON DUPLICATE KEY UPDATE `requests` = `requests` + 1";
mysql_query($sql);
}
//Gets the total number of requests that have been made today.
function get_todays_api_requests($ip){
$ip = mysql_real_escape_string($ip);
$total = mysql_query("SELECT `requests` FROM `api_log` WHERE `ip` = INET_ATON('{$ip}') AND `date` = DATE(NOW())");
return (mysql_num_rows($total) == 1) ? mysql_result($toal, 0) : 0;
}
?>
../api.php
<?php
require('class/api.php');
header('Content-type: application/json');
if(isset($_POST['username'])&&isset($_POST['password'])){
$errors = array();
$username = (empty($errors)) ? $_SESSION['scmsacc'] = $_POST['username'] : false;
echo json_encode(array(
'username' => $username,
'errors' => $errors,
));
log_api_request($_SERVER['REMOTE_ADDR']);
}else{
echo json_encode(array(
'login' => 'Allows Interactivity With The Content Management System to Other Web Systems.',
));
}
?>
/api_test/api_interface.inc.php:
<?php
class surfcms {
const API_URL = 'http://localhost/projects/surf-cms/api.php';
// DONT EDIT FROM HERE
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)));
}
//TOO HERE :L <-- the connection is true just passing the username and password is the problem and checking for the result...
//Grab the Indevidual Content and Put into a $decode->{$varible} but dont know how?
public static function get_content(){
return json_decode(self::send_post_data({$GOD_KNOWS}}));
}
}
?>
/api_test/index.php:
<?php include('api_interface.inc.php'); session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php
print_r(surfcms::get_contents() . "<br />");
?>
<form action="" method="POST">
Username: <input type="text" name="username" /> <br />
Password: <input type="password" name="password" /> <input type="submit" />
</form>
</body>
</html>
Help is Much Apprieciatedphpkid01