⚡
PromptJoy
  • Welcome to PromptJoy Preview
    • Quickstart Guide
  • API
    • API Creation
    • API Usage
      • Java
      • Go
      • C#
      • JavaScript
      • PHP
      • Python
      • Python (Django)
      • Ruby
      • Ruby on Rails
      • Rust
      • TypeScript
  • Tutorials
    • Data Transformation/Enrichment
    • Building A Book Search Engine with PromptJoy and Next.js
  • Integration
    • Internal Data Sources
Powered by GitBook
On this page
  1. API
  2. API Usage

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.

PreviousJavaScriptNextPython

Last updated 1 year ago