PHP

In PHP, we'll be using the cURL library to send HTTP requests. Here's how you can use the book search API with PHP:

<?php
function searchBook($query) {
    $url = 'https://api.promptjoy.com/api/mVMCpq';
    $apiKey = getenv('PROMPTJOY_API_KEY');
    
    $ch = curl_init();
    
    $postData = json_encode(array('query' => $query));
    
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'x-api-key: ' . $apiKey
        ),
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $postData
    );
    
    curl_setopt_array($ch, $options);
    
    $response = curl_exec($ch);
    
    if (!$response) {
        die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
    }
    
    curl_close($ch);
    
    return json_decode($response, true);
}

$books = searchBook('your_search_term');
foreach ($books as $book) {
    echo "Title: " . $book['title'] . ", Author: " . $book['author'] . "\n";
}
?>

Replace "your_search_term" with the book you're searching for. This function will return an array of books.

Remember to handle exceptions and errors as needed in your actual application code.

Note: In this example, the API key is retrieved from environment variables for security reasons. Ensure that you've set the PROMPTJOY_API_KEY environment variable in your environment where this code will be executed.

Last updated