Php Package
MiraQuotePHP is a simple PHP package that provides access to the mirageks quote api from different categories, allowing easy integration into any PHP project without even needing to use composer or anything you hate. While you can, you should not.
What you expect​
- Get random quotes.
- Request quotes by category or author.
- Retrieve multiple quotes at once.
- Lightweight and easy to integrate.
Installation​
-
Clone the repository or download the package:
git clone https://github.com/CallMeGodsent/miraquote.git
or visit https://github.com/CallMeGodsent/miraquote to download directly.
-
Require the package in your PHP script:
require_once 'path/to/MiraQuotePHP/QuotesApiClient.php';
-
Composer Autoloading (optional): Please check the composer section if this is what you want. Cant find Composer? It might be down
Usage​
1. Initialize the Client​
To start using the API Package, instantiate the QuotesApiClient
with your API key from your Miragek account dashboard:
use MiraQuotePHP\QuotesApiClient;
$client = new QuotesApiClient('your_api_key_here');
For more code examples, please refer to the downloaded file. For now, lets continue the read.
2. Request Random Quotes​
Request a Single Quote​
$randomQuote = $client->getRandomQuote();
Request Multiple Quotes​
You can also get many quotes from any specific category:
$numberOfQuotes = 5;
$loveQuotes = $client->getRandomQuote('love', null, $numberOfQuotes);
Request Quotes by Author​
Lets say you want quotes by a specific author. You can do it by passing their name as the second argument:
$johnQuotes = $client->getRandomQuote(null, 'john', 2);
3. Example Code​
This is how you can do it:
<?php
require_once 'MiraQuotePHP/QuotesApiClient.php';
use MiraQuotePHP\QuotesApiClient;
$client = new QuotesApiClient('your_api_key_here');
try {
$numberOfQuotes = 5;
$loveQuotes = $client->getRandomQuote('love', null, $numberOfQuotes);
echo "Love Quotes:\n";
if (!empty($loveQuotes)) {
foreach ($loveQuotes as $quote) {
echo "Quote: " . ($quote['quote'] ?? 'No quote') . "\n";
echo "Author: " . ($quote['author'] ?? 'Unknown author') . "\n\n";
}
} else {
echo "No love quotes found.\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Error Handling​
Make sure to wrap the API calls in a try-catch
blocks to handle any unexpected error gracefully.
try {
$quote = $client->getRandomQuote();
} catch (Exception $e) {
echo "Error fetching quote: " . $e->getMessage();
}
API Reference​
getRandomQuote($category = null, $author = null, $limit = 1)
​
- $category: (Optional) The category of quotes (e.g., 'love', 'happiness').
- $author: (Optional) Get quotes by author.
- $limit: (Optional) The number of quotes to retrieve (default is 1).
Example Usage​
// Fetch a single random quote
$randomQuote = $client->getRandomQuote();
// Fetch 3 quotes from the "love" category
$loveQuotes = $client->getRandomQuote('love', null, 3);
// Fetch 2 quotes by author "John"
$johnQuotes = $client->getRandomQuote(null, 'john', 2);
Requirements​
- PHP 7+
License​
The package or whatever you see here is CC0. You are free to use without credit.
Contributing​
Feel free to submit issues or pull requests.
Credits​
Created and maintained by Akpovi Godsent.