Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition of v1/getfoxes endpoint (#11)
* 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
1 parent
6c2f5da
commit b65ab99
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |