Code
You should be making a GET request to the API endpoint, not a POST request. Here's how the corrected PHP example would look:
API Endpoints Request
Get request to fetch users
PHP Example:
<?php
// GET random-users API endpoint in PHP
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// API URL to fetch random users
$url = 'https://thedocs.loma.com.ng/api.php/random-users';
// Initialize cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$response = curl_exec($ch);
// Check for errors
if(curl_errno($ch)) {
echo json_encode(["message" => "Error fetching data"]);
} else {
// Return the response from the API
echo $response;
}
// Close cURL session
curl_close($ch);
}
?>
Node.js Example:
const express = require('express');
const app = express();
const axios = require('axios');
app.get('/random-users', (req, res) => {
// API URL to fetch random users
const url = 'https://thedocs.loma.com.ng/api.php/random-users';
axios.get(url)
.then(response => {
// Send the response from the API back to the client
res.json(response.data);
})
.catch(error => {
// Handle error
res.status(500).json({ message: 'Error fetching data' });
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
2. Posts API Endpoint
GET request to fetch random posts
PHP Example:
<?php
// GET random-posts API endpoint in PHP
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// API URL to fetch random posts
$url = 'https://thedocs.loma.com.ng/api.php/random-posts';
// Initialize cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$response = curl_exec($ch);
// Check for errors
if(curl_errno($ch)) {
echo json_encode(["message" => "Error fetching data"]);
} else {
// Return the response from the API
echo $response;
}
// Close cURL session
curl_close($ch);
}
?>
Node.js Example:
const express = require('express');
const app = express();
const axios = require('axios');
app.get('/random-posts', (req, res) => {
// API URL to fetch random posts
const url = 'https://thedocs.loma.com.ng/api.php/random-posts';
axios.get(url)
.then(response => {
// Send the response from the API back to the client
res.json(response.data);
})
.catch(error => {
// Handle error
res.status(500).json({ message: 'Error fetching data' });
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
2. Images API Endpoint
GET request to fetch random images
PHP Example:
<?php
// GET random-images API endpoint in PHP
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// API URL to fetch random images
$url = 'https://thedocs.loma.com.ng/api.php/random-images';
// Initialize cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$response = curl_exec($ch);
// Check for errors
if(curl_errno($ch)) {
echo json_encode(["message" => "Error fetching data"]);
} else {
// Return the response from the API
echo $response;
}
// Close cURL session
curl_close($ch);
}
?>
Node.js Example:
const express = require('express');
const app = express();
const axios = require('axios');
app.get('/random-images', (req, res) => {
// API URL to fetch random images
const url = 'https://thedocs.loma.com.ng/api.php/random-images';
axios.get(url)
.then(response => {
// Send the response from the API back to the client
res.json(response.data);
})
.catch(error => {
// Handle error
res.status(500).json({ message: 'Error fetching data' });
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});