Skip to content

Commit

Permalink
Addition of v1/getfoxes endpoint (#11)
Browse files Browse the repository at this point in the history
* Add feature of multiple foxes return

* Fix sanitisation and standards

* Update index.php

* Nicer declaration of the foor loop (line 15)
* Removed redundant statement (line 16)

Co-authored-by: xinitrc <legiostels@gmail.com>
  • Loading branch information
naveen521kk and xinitrc-dev committed Jul 3, 2020
1 parent 6c2f5da commit b65ab99
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions api/v1/getfoxes/index.php
@@ -0,0 +1,51 @@
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

$MAX_ALLOWED_COUNT = 20;
$valid_request = FALSE;
$files = glob('../../../images/*');
if ($files and isset($_GET['count'])) {
$FOX_NUM = count($files);
if (ctype_digit($_GET['count'])){
if ($_GET['count'] <= $MAX_ALLOWED_COUNT and $_GET['count'] > 0){
$foxNums = array();
$image_paths = array();
$links = array();
for ($x = 0; $x < $_GET["count"]; $x++) {
do {
$random_number = rand(1, $FOX_NUM);
} while (in_array( $random_number ,$foxNums ));
array_push($foxNums,$random_number);
array_push($image_paths,'https://randomfox.ca/images/'.$random_number.'.jpg');
array_push($links,'https://randomfox.ca/?i='.$random_number);
$valid_request = TRUE;
}
} else {
if ($_GET['count'] <= 0){
header('HTTP/1.1 420 Need to specify COUNT between 0 and '.$MAX_ALLOWED_COUNT.'');
$error_msg = '\'count\' needs to be specified between 0 and '.$MAX_ALLOWED_COUNT.'';
} else {
header('HTTP/1.1 420 Need to specify COUNT within'.$MAX_ALLOWED_COUNT.'');
$error_msg = '\'count\' needs to be specified within '.$MAX_ALLOWED_COUNT.'';
}
}
} else {
header('HTTP/1.1 420 Need to specify COUNT 0 and '.$MAX_ALLOWED_COUNT.'');
$error_msg = '\'count\' needs to be specified 0 and '.$MAX_ALLOWED_COUNT.'';
}
} else {
$foxNums = null;
$image_paths = null;
$links = null;
if (ctype_digit($_GET['count'])){}
header('HTTP/1.1 420 Need to specify COUNT');
$error_msg = '\'count\' needs to be specified';
}
if ($valid_request == TRUE){
$data = array('images' => $image_paths, 'links' => $links);

} else {
$data = array('error' => $error_msg,'status' => '420');
}
echo json_encode($data);

0 comments on commit b65ab99

Please sign in to comment.