{"id":51052566,"url":"https://github.com/rakotomandimby/php-ai-raw-api-client","last_synced_at":"2026-06-22T18:32:08.683Z","repository":{"id":364021719,"uuid":"1265987557","full_name":"rakotomandimby/php-ai-raw-api-client","owner":"rakotomandimby","description":"PHP Raw REST API client for major AI Model providers","archived":false,"fork":false,"pushed_at":"2026-06-11T10:30:43.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-11T11:07:38.592Z","etag":null,"topics":["bare-php","bare-rest-api-client","php-anthropic","php-google-ai","php-openai"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rakotomandimby.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-06-11T08:30:21.000Z","updated_at":"2026-06-11T10:30:47.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/rakotomandimby/php-ai-raw-api-client","commit_stats":null,"previous_names":["rakotomandimby/php-ai-raw-api-client"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/rakotomandimby/php-ai-raw-api-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakotomandimby%2Fphp-ai-raw-api-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakotomandimby%2Fphp-ai-raw-api-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakotomandimby%2Fphp-ai-raw-api-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakotomandimby%2Fphp-ai-raw-api-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rakotomandimby","download_url":"https://codeload.github.com/rakotomandimby/php-ai-raw-api-client/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakotomandimby%2Fphp-ai-raw-api-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34661703,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-22T02:00:06.391Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bare-php","bare-rest-api-client","php-anthropic","php-google-ai","php-openai"],"created_at":"2026-06-22T18:32:08.600Z","updated_at":"2026-06-22T18:32:08.659Z","avatar_url":"https://github.com/rakotomandimby.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-ai-raw-api-client\n\nSmall standalone PHP examples for calling raw HTTP APIs from major LLM providers without a framework or SDK.\n\n## Purpose\n\nThis repository shows how to:\n\n- send a prompt and an optional system instruction to several AI providers\n- authenticate requests with provider-specific API keys\n- build JSON payloads manually\n- execute HTTPS POST requests with cURL\n- decode JSON responses and surface API or transport failures as `Exception`s\n- extract generated text from each provider's response schema\n\nIt is useful if you want a very small PHP starting point for provider integration, debugging raw requests, or comparing response formats across vendors.\n\n## Repository structure\n\n- `/openai/openai-client.php` — calls the OpenAI `/v1/responses` endpoint hardcoded in this repo\n- `/openai/example.php` — runnable OpenAI example\n- `/anthropic/anthropic-client.php` — calls the Anthropic Messages API\n- `/anthropic/example.php` — runnable Anthropic example\n- `/google-ai/google-ai-client.php` — calls the Google AI `/v1beta/interactions` endpoint hardcoded in this repo\n- `/google-ai/example.php` — runnable Google AI example\n\n## What the code does\n\nEach client file exposes one function:\n\n- `call_openai_response(string $instruction, string $prompt, string $model, ?string $apiKey = null): array`\n- `call_anthropic_message(string $instruction, string $prompt, string $model, ?string $apiKey = null): array`\n- `call_gemini_interaction(string $instruction, string $prompt, string $model, ?string $apiKey = null): array`\n\nAll three functions follow the same pattern:\n\n1. resolve the API key from an argument or environment variable\n2. build a provider-specific request payload\n3. send a POST request with cURL\n4. decode the JSON body into a PHP array\n5. throw an exception on missing credentials, cURL failure, invalid JSON, or non-2xx API responses\n6. return the decoded response for the caller to interpret\n\n## Requirements\n\n- PHP 8+\n- PHP cURL extension enabled\n- outbound network access to the provider API\n- a valid API key for the provider you want to call\n\n## Provider configuration\n\n| Provider | Client function | API endpoint | Environment variable used by the code |\n| --- | --- | --- | --- |\n| OpenAI | `call_openai_response()` | `https://api.openai.com/v1/responses` | `OPENAI_API_KEY` |\n| Anthropic | `call_anthropic_message()` | `https://api.anthropic.com/v1/messages` | `ANTHROPIC_API_KEY` |\n| Google AI | `call_gemini_interaction()` | `https://generativelanguage.googleapis.com/v1beta/interactions` | `GOOGLEAI_API_KEY` |\n\n## Default request behavior\n\nThe scripts keep the interface intentionally small and hardcode some generation settings:\n\n- OpenAI: `temperature=0.7`, `top_p=0.95`\n- Anthropic: `temperature=0.7`, `max_tokens=2048`\n- Google AI: `temperature=0.7`, `top_p=0.95`, `max_output_tokens=2048`\n\nIf you need other parameters, edit the payload in the corresponding client file.\n\n## How to use\n\n### 1. Pick a provider\n\nUse the directory that matches the service you want to call.\n\n### 2. Set the API key\n\nExamples:\n\n```bash\nexport OPENAI_API_KEY=\"your-openai-key\"\nexport ANTHROPIC_API_KEY=\"your-anthropic-key\"\nexport GOOGLEAI_API_KEY=\"your-google-ai-key\"\n```\n\n### 3. Run one of the included examples\n\n```bash\nphp openai/example.php\nphp anthropic/example.php\nphp google-ai/example.php\n```\n\nEach example:\n\n- loads the local client file with `require_once`\n- defines an instruction, a prompt, and a model name\n- calls the provider function\n- walks the provider response structure\n- prints the final generated text to stdout\n\n## Using the client functions in your own code\n\n### OpenAI\n\n```php\n\u003c?php\nrequire_once __DIR__ . '/openai/openai-client.php';\n\n$response = call_openai_response(\n    'You are a concise assistant.',\n    'Summarize the purpose of this repository.',\n    'gpt-5.4-mini'\n);\n```\n\n### Anthropic\n\n```php\n\u003c?php\nrequire_once __DIR__ . '/anthropic/anthropic-client.php';\n\n$response = call_anthropic_message(\n    'You are a concise assistant.',\n    'Summarize the purpose of this repository.',\n    'claude-haiku-4-5'\n);\n```\n\n### Google AI\n\n```php\n\u003c?php\nrequire_once __DIR__ . '/google-ai/google-ai-client.php';\n\n$response = call_gemini_interaction(\n    'You are a concise assistant.',\n    'Summarize the purpose of this repository.',\n    'gemini-3.5-flash'\n);\n```\n\nBecause each function returns the full decoded API payload, your application can either:\n\n- extract only the generated text\n- inspect IDs, metadata, and intermediate output objects\n- log raw responses while integrating or debugging\n\n## Response parsing notes\n\nThe bundled example files extract text from the response shapes expected by the current scripts:\n\n- OpenAI: `output[*].content[*].text` when `type === \"output_text\"`\n- Anthropic: `content[*].text` when `type === \"text\"`\n- Google AI: `steps[*].content[*].text` when the step type is `model_output`\n\nThese paths describe the structures handled by the code in this repository. If the provider changes its schema or you request other output types, update the extraction logic in the example or in your application code.\n\n## Error handling\n\nThe client functions throw `Exception` when:\n\n- the API key is missing\n- cURL cannot initialize or execute the request\n- the response body is not valid JSON\n- the provider returns a non-success HTTP status\n\nWrap calls in `try/catch` if you want to show user-friendly messages or retry failed requests.\n\n## Limitations\n\nThis repository is intentionally minimal:\n\n- no Composer package or autoloading\n- no retries or backoff\n- no streaming support\n- no typed response objects\n- no automated tests\n- no abstraction layer across providers beyond similar function signatures\n\n## When to extend it\n\nThis codebase is a good base if you want to add:\n\n- configurable generation parameters\n- shared helper functions for cURL and error handling\n- response normalization across providers\n- test coverage with mocked HTTP responses\n- Composer packaging for reuse in other projects\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frakotomandimby%2Fphp-ai-raw-api-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frakotomandimby%2Fphp-ai-raw-api-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frakotomandimby%2Fphp-ai-raw-api-client/lists"}