https://github.com/rakotomandimby/php-ai-raw-api-client
PHP Raw REST API client for major AI Model providers
https://github.com/rakotomandimby/php-ai-raw-api-client
bare-php bare-rest-api-client php-anthropic php-google-ai php-openai
Last synced: 25 days ago
JSON representation
PHP Raw REST API client for major AI Model providers
- Host: GitHub
- URL: https://github.com/rakotomandimby/php-ai-raw-api-client
- Owner: rakotomandimby
- Created: 2026-06-11T08:30:21.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2026-06-11T10:30:43.000Z (about 1 month ago)
- Last Synced: 2026-06-11T11:07:38.592Z (about 1 month ago)
- Topics: bare-php, bare-rest-api-client, php-anthropic, php-google-ai, php-openai
- Language: PHP
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# php-ai-raw-api-client
Small standalone PHP examples for calling raw HTTP APIs from major LLM providers without a framework or SDK.
## Purpose
This repository shows how to:
- send a prompt and an optional system instruction to several AI providers
- authenticate requests with provider-specific API keys
- build JSON payloads manually
- execute HTTPS POST requests with cURL
- decode JSON responses and surface API or transport failures as `Exception`s
- extract generated text from each provider's response schema
It is useful if you want a very small PHP starting point for provider integration, debugging raw requests, or comparing response formats across vendors.
## Repository structure
- `/openai/openai-client.php` — calls the OpenAI `/v1/responses` endpoint hardcoded in this repo
- `/openai/example.php` — runnable OpenAI example
- `/anthropic/anthropic-client.php` — calls the Anthropic Messages API
- `/anthropic/example.php` — runnable Anthropic example
- `/google-ai/google-ai-client.php` — calls the Google AI `/v1beta/interactions` endpoint hardcoded in this repo
- `/google-ai/example.php` — runnable Google AI example
## What the code does
Each client file exposes one function:
- `call_openai_response(string $instruction, string $prompt, string $model, ?string $apiKey = null): array`
- `call_anthropic_message(string $instruction, string $prompt, string $model, ?string $apiKey = null): array`
- `call_gemini_interaction(string $instruction, string $prompt, string $model, ?string $apiKey = null): array`
All three functions follow the same pattern:
1. resolve the API key from an argument or environment variable
2. build a provider-specific request payload
3. send a POST request with cURL
4. decode the JSON body into a PHP array
5. throw an exception on missing credentials, cURL failure, invalid JSON, or non-2xx API responses
6. return the decoded response for the caller to interpret
## Requirements
- PHP 8+
- PHP cURL extension enabled
- outbound network access to the provider API
- a valid API key for the provider you want to call
## Provider configuration
| Provider | Client function | API endpoint | Environment variable used by the code |
| --- | --- | --- | --- |
| OpenAI | `call_openai_response()` | `https://api.openai.com/v1/responses` | `OPENAI_API_KEY` |
| Anthropic | `call_anthropic_message()` | `https://api.anthropic.com/v1/messages` | `ANTHROPIC_API_KEY` |
| Google AI | `call_gemini_interaction()` | `https://generativelanguage.googleapis.com/v1beta/interactions` | `GOOGLEAI_API_KEY` |
## Default request behavior
The scripts keep the interface intentionally small and hardcode some generation settings:
- OpenAI: `temperature=0.7`, `top_p=0.95`
- Anthropic: `temperature=0.7`, `max_tokens=2048`
- Google AI: `temperature=0.7`, `top_p=0.95`, `max_output_tokens=2048`
If you need other parameters, edit the payload in the corresponding client file.
## How to use
### 1. Pick a provider
Use the directory that matches the service you want to call.
### 2. Set the API key
Examples:
```bash
export OPENAI_API_KEY="your-openai-key"
export ANTHROPIC_API_KEY="your-anthropic-key"
export GOOGLEAI_API_KEY="your-google-ai-key"
```
### 3. Run one of the included examples
```bash
php openai/example.php
php anthropic/example.php
php google-ai/example.php
```
Each example:
- loads the local client file with `require_once`
- defines an instruction, a prompt, and a model name
- calls the provider function
- walks the provider response structure
- prints the final generated text to stdout
## Using the client functions in your own code
### OpenAI
```php