How To Build Your Own API Using PHP

I gotchu fam
Remember when this was you?
There’s plenty of data to go around
You’re going to be able to produce the same output
You da real MVP
<?phpheader(‘Content-Type: application/json’);$results = array();
$length = $_GET[“length”];if($_GET[“min”]) {
$min = $_GET[“min”];
} else $min = 1;
if($_GET[“max”]) {
$max = $_GET[“max”];
} else $max = $length;
$sort = “”;if($_GET[“sort”]) {
$sort = $_GET[“sort”];
}
if($_GET[“sort”]) {
for ($i=0;$i<$length;$i++) {
$results[] = rand($min,$max);
}
if ($sort==”asc”) {
sort($results);
} elseif ($sort==”desc”) {
rsort($results);
}
$endArray = array(‘array’ => $results);echo json_encode($endArray);?>
fetch(‘https://cors-anywhere.herokuapp.com/https://www.crazyhappyfuntime.com/rando/?length=10000&min=1&inc=true')
.then(res => res.json())
.then(data => {
document.getElementById(“output”).innerHTML = data.array;
})
.catch(function(error) {
console.log(error.message);
})
Have fun!
<?phpheader(‘Content-Type: application/json’);$results = array();$length = $_GET[“length”];if($_GET[“min”]) {
$min = $_GET[“min”];
} else $min = 1;
if($_GET[“max”]) {
$max = $_GET[“max”];
} else $max = $length;
$sort = “”;if($_GET[“sort”]) {
$sort = $_GET[“sort”];
}
for ($i=0;$i<$length;$i++) {
$results[] = rand($min,$max);
}
if ($sort==”asc”) {
sort($results);
} elseif ($sort==”desc”) {
rsort($results);
}
$endArray = array(‘array’ => $results);echo json_encode($endArray);?>
All this fun makes me thirsty

--

--

--

4a 75 73 74 20 61 6e 6f 74 68 65 72 20 63 6f 6d 70 75 74 65 72 20 6e 65 72 64 20 77 69 74 68 20 61 20 62 6c 6f 67

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The What, Why and Wow! Behind the CoreOS Container Linux

KWoC Project Report

Framework 1: Secondary Fire (part 2)

Managing colors in SASS

Why from therapy to coding

Python Collections Module

SQL vs NoSQL: Determining What’s Right For You In 2018

A Hanoi Tower Variation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
crashdaddy

crashdaddy

4a 75 73 74 20 61 6e 6f 74 68 65 72 20 63 6f 6d 70 75 74 65 72 20 6e 65 72 64 20 77 69 74 68 20 61 20 62 6c 6f 67

More from Medium

Stripe ACH implementation in laravel

Code your Laravel projects everywhere with Gitpod

Node.js or PHP — Which is better?

How To Enable PHP Debugging with XDebug and Visual Studio Code (Windows and Xampp)