HomeDocsPHP Integration

PHP Integration Tutorial

Add AI-powered semantic search to your PHP website in 6 simple steps

Available now10 minute readPHP 7.4+

What you'll build

By the end of this tutorial, you'll have a working semantic search that understands natural language queries. Users searching for "gift for my dad" will find relevant products even if they don't contain those exact keywords.

Prerequisites

Before you start, make sure you have:

  • PHP 7.4 or higher – Check with php -v
  • file_get_contents enabled – Usually enabled by default

No installation required!

This tutorial uses native PHP – no Composer dependencies needed.

1Sign Up & Get API Key

1. Create your free account

Go to queryra.com/signup

Free plan includes: 100 records and 500 searches/month

2. Get your API key

  1. Log in to your dashboard
  2. Go to API Keys tab
  3. Click Create API Key
  4. Copy the key (starts with sk_live_)

Important

Keep your API key secret! Don't commit it to Git or share it publicly.

2Test Connection

Let's verify your API key works. Create test-connection.php:

test-connection.php
<?php
// test-connection.php
define('API_KEY', 'sk_live_YOUR_API_KEY_HERE');

// Step 1: Check server health
$healthUrl = 'https://queryra.com/health';
$response = @file_get_contents($healthUrl);
$data = json_decode($response, true);

if ($data && isset($data['status']) && $data['status'] === 'healthy') {
    echo "✓ Server is healthy!\n";
    echo "  Version: " . $data['version'] . "\n";
    echo "  Model: " . $data['embedding_model'] . "\n\n";
} else {
    echo "✗ Server health check failed!\n";
    exit(1);
}

// Step 2: Validate API key
$statsUrl = 'https://queryra.com/api/v1/stats?key=' . API_KEY;
$response = @file_get_contents($statsUrl);
$data = json_decode($response, true);

if (isset($data['email'])) {
    echo "✓ API Key is valid!\n";
    echo "  Email: " . $data['email'] . "\n";
    echo "  Plan: " . strtoupper($data['plan']) . "\n";
    echo "  Records: " . $data['products_indexed'] . " / " . $data['products_limit'] . "\n";
    echo "  Searches this month: " . $data['searches_this_month'] . " / " . $data['searches_limit'] . "\n";
} else {
    echo "✗ API Key validation failed!\n";
    if (isset($data['detail'])) {
        echo "  Error: " . $data['detail'] . "\n";
    }
    exit(1);
}
?>

Run it:

$ php test-connection.php

Expected output:

✓ Server is healthy!
  Version: 1.0.0
  Model: all-MiniLM-L6-v2

✓ API Key is valid!
  Email: your@email.com
  Plan: FREE
  Records: 0 / 100
  Searches this month: 0 / 500

Connection successful!

Your API key is working. Let's import some data.

3Bulk Import Records

Now let's add some products to your account. Create bulk-import.php:

bulk-import.php
<?php
// bulk-import.php
define('API_KEY', 'sk_live_YOUR_API_KEY_HERE');

// Prepare your products
$products = [
    [
        'id' => 'prod_001',
        'name' => 'Gaming Laptop',
        'description' => 'High-performance laptop for gaming and programming. RTX 4060, 16GB RAM, 1TB SSD.',
        'price' => 1299.99,
        'url' => 'https://yourstore.com/products/gaming-laptop',
        'image_url' => 'https://yourstore.com/images/laptop.jpg',
        'stock' => 15,
        'margin' => 0.75  // Optional: 0-1 range for PRO/Business product boost
    ],
    [
        'id' => 'prod_002',
        'name' => 'Wireless Headphones',
        'description' => 'Noise-cancelling wireless headphones with 30-hour battery life. Perfect for music and calls.',
        'price' => 199.99,
        'url' => 'https://yourstore.com/products/headphones',
        'image_url' => 'https://yourstore.com/images/headphones.jpg',
        'stock' => 50,
        'margin' => 0.60
    ],
    // Add more products...
];

// Send bulk request
$url = 'https://queryra.com/api/v1/records/bulk?key=' . API_KEY;
$payload = ['products' => $products];  // Important: wrap in 'products' key

$options = [
    'http' => [
        'method' => 'POST',
        'header' => 'Content-Type: application/json',
        'content' => json_encode($payload),
        'ignore_errors' => true
    ]
];

$context = stream_context_create($options);
$response = @file_get_contents($url, false, $context);
$data = json_decode($response, true);

if (isset($data['status']) && $data['status'] === 'success') {
    echo "✓ Bulk import successful!\n";
    echo "  Added: " . $data['added'] . " records\n";
    echo "  Total in account: " . $data['total_records'] . "\n";
} else {
    echo "✗ Import failed!\n";
    if (isset($data['detail'])) {
        echo "  Error: " . $data['detail'] . "\n";
    }
}
?>

Required fields:

  • id – Unique identifier (string)
  • name – Product name (string)
  • description – Product description (string, used for semantic search)
  • price – Price (number)

Optional fields:

  • url – Product URL (string)
  • image_url – Image URL (string)
  • stock – Stock quantity (number)
  • margin – Product boost priority, 0-1 range (PRO/Business plans only)
Note: The payload must be wrapped in a products key as shown above. You can send up to 100 products per request.

4Manage Records in Dashboard

View your records:

What you can do:

  • Delete records – Remove individual records or bulk delete
  • View stats – See total records, searches used, and plan limits

You can also check records programmatically:

check-records.php
<?php
// check-records.php
define('API_KEY', 'sk_live_YOUR_API_KEY_HERE');

$url = 'https://queryra.com/api/v1/records/stats?key=' . API_KEY;
$response = @file_get_contents($url);
$data = json_decode($response, true);

echo "Records: " . $data['total_records'] . " / " . $data['limit'] . "\n";
echo "Synced: " . $data['synced_records'] . "\n";
echo "Pending sync: " . $data['pending_sync'] . "\n";
?>

5Sync Records (Important!)

Before you can search, you must sync your records.

Syncing is the process where your products are converted into AI embeddings (vector representations) that enable semantic search. This happens in the dashboard:

  1. Go to queryra.com/dashboard
  2. Click the "Sync Records" button
  3. Wait for the sync to complete (you'll see a progress indicator)

Sync is a computationally expensive process

  • 200 records take approximately 60 seconds
  • Free plan can sync once per month due to resource limitations
  • Paid plans have more frequent sync intervals (see pricing)

After syncing, your records are searchable! You can verify sync status:

check-sync.php
<?php
define('API_KEY', 'sk_live_YOUR_API_KEY_HERE');

$url = 'https://queryra.com/api/v1/records/stats?key=' . API_KEY;
$response = @file_get_contents($url);
$data = json_decode($response, true);

if ($data['synced_records'] > 0) {
    echo "✓ Sync complete! " . $data['synced_records'] . " records are searchable.\n";
} else {
    echo "⚠ No records synced yet. Please sync in dashboard.\n";
}
?>
Once synced, you're ready to search!

6Search Your Data

Now you can search! Create search.php:

search.php
<?php
// search.php
define('API_KEY', 'sk_live_YOUR_API_KEY_HERE');
define('QUERY', 'gift for my dad');  // Change this to test different queries

$url = 'https://queryra.com/api/v1/search?' . http_build_query([
    'key' => API_KEY,
    'q' => QUERY,
    'limit' => 5
]);

$response = @file_get_contents($url);
$data = json_decode($response, true);

echo "Search: \"" . QUERY . "\"\n\n";

foreach ($data['results'] as $i => $result) {
    echo ($i + 1) . ". " . $result['name'] . "\n";
    echo "   Score: " . round($result['relevance_score'], 3) . "\n";
    echo "   " . substr($result['description'], 0, 80) . "...\n\n";
}
?>

Run it:

$ php search.php

Expected output:

Search: "gift for my dad"

1. Historical War Game
   Score: 0.892
   Strategic board game about WWII. Perfect for history enthusiasts. 2-4 players...

2. Vintage Compass Set
   Score: 0.845
   Classic brass compass with wooden case. Great gift for adventurous dads who...

3. Classic Literature Collection
   Score: 0.821
   Leather-bound collection of timeless classics. Perfect for book-loving fathers...

Try these natural language queries:

  • "gift for my dad who loves history"
  • "something quick to play during lunch break"
  • "affordable laptop for students"
  • "outdoor activity for family weekend"

Congratulations!

You now have working semantic search that understands natural language.

Next Steps

You've successfully integrated semantic search! Here are some things to try next:

  • Monitor usage: Check your dashboard to see search analytics and remaining limits
  • Upgrade your plan: If you need more records or searches, see pricing plans
  • WordPress plugin: If you use WordPress/WooCommerce, try our official plugin
  • API documentation: Explore the full API at queryra.com/api/docs

Troubleshooting

Search returns no results:

  • Make sure you synced your records in the dashboard (Step 5)
  • Check that synced_records > 0 via /api/v1/records/stats
  • Verify your API key is correct

Bulk import fails:

  • Check you're wrapping products in a products key
  • Verify all required fields are present: id, name, description, price
  • Check you haven't exceeded your plan limit (see dashboard)

Still need help?

Last updated: January 30, 2026