I’m trying to write a php script that can save the player progress using apache as a server and fiddler to redirect the requests to localhost, this is the script that i wrote but it doesn’t work, the gamedata.json file is stored in htdocswww.cartoonnetwork.compapiv2useratepicquestgamedata.json
<code><?php
// Define the path to the JSON file
define('DATA_FILE', 'www.cartoonnetwork.com/papi/v2/user/atepicquest/gamedata.json');
// Function to handle GET requests for game data
function getGameData() {
// Check if the file exists and read data from it
if (file_exists(DATA_FILE)) {
$data = file_get_contents(DATA_FILE);
} else {
// Default data if file does not exist
$data = json_encode(['version' => 0, 'data' => []]);
}
// Set the content type to JSON
header('Content-Type: application/json');
// Send JSON response
echo $data;
}
// Function to handle POST requests for game data
function postGameData() {
// Get the JSON input from the request body
$json = file_get_contents('php://input');
$gamedata = json_decode($json, true);
// Increment version from query string, default to 0 if not provided
$version = isset($_GET['version']) ? intval($_GET['version']) + 1 : 1;
// Create save data structure
$savedata = [
'version' => $version,
'data' => $gamedata
];
// Convert save data to JSON
$jsonData = json_encode($savedata);
// Write game data to file
file_put_contents(DATA_FILE, $jsonData);
// Set the content type to JSON
header('Content-Type: application/json');
// Send JSON response
echo $jsonData;
}
// Determine the request URI and method
$requestUri = $_SERVER['REQUEST_URI'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
// Route requests based on the URI and HTTP method
if ($requestUri == 'www.cartoonnetwork.com/papi/v2/user/atepicquest/' && $requestMethod == 'GET') {
getGameData();
} elseif ($requestUri == 'www.cartoonnetwork.com/papi/v2/user/atepicquest/' && $requestMethod == 'POST') {
postGameData();
} else {
// Handle 404 Not Found
header("HTTP/1.0 404 Not Found");
echo "Endpoint not found.";
}
?>
</code>
<code><?php
// Define the path to the JSON file
define('DATA_FILE', 'www.cartoonnetwork.com/papi/v2/user/atepicquest/gamedata.json');
// Function to handle GET requests for game data
function getGameData() {
// Check if the file exists and read data from it
if (file_exists(DATA_FILE)) {
$data = file_get_contents(DATA_FILE);
} else {
// Default data if file does not exist
$data = json_encode(['version' => 0, 'data' => []]);
}
// Set the content type to JSON
header('Content-Type: application/json');
// Send JSON response
echo $data;
}
// Function to handle POST requests for game data
function postGameData() {
// Get the JSON input from the request body
$json = file_get_contents('php://input');
$gamedata = json_decode($json, true);
// Increment version from query string, default to 0 if not provided
$version = isset($_GET['version']) ? intval($_GET['version']) + 1 : 1;
// Create save data structure
$savedata = [
'version' => $version,
'data' => $gamedata
];
// Convert save data to JSON
$jsonData = json_encode($savedata);
// Write game data to file
file_put_contents(DATA_FILE, $jsonData);
// Set the content type to JSON
header('Content-Type: application/json');
// Send JSON response
echo $jsonData;
}
// Determine the request URI and method
$requestUri = $_SERVER['REQUEST_URI'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
// Route requests based on the URI and HTTP method
if ($requestUri == 'www.cartoonnetwork.com/papi/v2/user/atepicquest/' && $requestMethod == 'GET') {
getGameData();
} elseif ($requestUri == 'www.cartoonnetwork.com/papi/v2/user/atepicquest/' && $requestMethod == 'POST') {
postGameData();
} else {
// Handle 404 Not Found
header("HTTP/1.0 404 Not Found");
echo "Endpoint not found.";
}
?>
</code>
<?php
// Define the path to the JSON file
define('DATA_FILE', 'www.cartoonnetwork.com/papi/v2/user/atepicquest/gamedata.json');
// Function to handle GET requests for game data
function getGameData() {
// Check if the file exists and read data from it
if (file_exists(DATA_FILE)) {
$data = file_get_contents(DATA_FILE);
} else {
// Default data if file does not exist
$data = json_encode(['version' => 0, 'data' => []]);
}
// Set the content type to JSON
header('Content-Type: application/json');
// Send JSON response
echo $data;
}
// Function to handle POST requests for game data
function postGameData() {
// Get the JSON input from the request body
$json = file_get_contents('php://input');
$gamedata = json_decode($json, true);
// Increment version from query string, default to 0 if not provided
$version = isset($_GET['version']) ? intval($_GET['version']) + 1 : 1;
// Create save data structure
$savedata = [
'version' => $version,
'data' => $gamedata
];
// Convert save data to JSON
$jsonData = json_encode($savedata);
// Write game data to file
file_put_contents(DATA_FILE, $jsonData);
// Set the content type to JSON
header('Content-Type: application/json');
// Send JSON response
echo $jsonData;
}
// Determine the request URI and method
$requestUri = $_SERVER['REQUEST_URI'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
// Route requests based on the URI and HTTP method
if ($requestUri == 'www.cartoonnetwork.com/papi/v2/user/atepicquest/' && $requestMethod == 'GET') {
getGameData();
} elseif ($requestUri == 'www.cartoonnetwork.com/papi/v2/user/atepicquest/' && $requestMethod == 'POST') {
postGameData();
} else {
// Handle 404 Not Found
header("HTTP/1.0 404 Not Found");
echo "Endpoint not found.";
}
?>
New contributor
Ayman AL-Azaab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.