Dino Geek, try to help you

How to connect to ChatGPT API with PHP?


To interact with the OpenAI GPT-3 model using PHP, you would need to make API calls. The following is a simple example of how to do this using the Guzzle HTTP client:

Firstly, you need to install Guzzle via composer:

```
composer require guzzlehttp/guzzle
```

Then this is the sample code:

```

require ‘vendor/autoload.php’;

use GuzzleHttp\Client;

$httpClient = new Client();

$response = $httpClient->post(‘https://api.openai.com/v1/engines/davinci-codex/completions’, [ ‘headers’ => [ ‘Content-Type’ => ‘application/json’, ‘Authorization’ => ‘Bearer YOUR_OPENAI_KEY_HERE’, ], ‘json’ => [ ‘prompt’ => ‘Translate the following English text to French: {}’, ‘max_tokens’ => 60, ],
]);

echo $response->getBody();
```

Make sure to replace `‘Bearer YOUR_OPENAI_KEY_HERE’` with your actual OpenAI key.

This PHP script creates a Guzzle HTTP client, and makes a `POST` request to the OpenAI API’s `/v1/engines/davinci-codex/completions` endpoint.

It sends JSON in the body of the request which includes the prompt and max\_tokens parameters required to call the chat models.

The Authorization header is set using `Bearer YOUR_OPENAI_KEY_HERE`, where `YOUR_OPENAI_KEY_HERE` is your OpenAI API key.

Finally, the response from OpenAI is printed to the console.

You might have to customize the endpoint(POST URL), model, prompt or other parameters(More details can be found in the OpenAI API documentation).

Please note that as of March 1st, 2023, you can only use the OpenAI API to make calls to GPT-3. The OpenAI Playground is no longer free to use and you will need to pay for each API call you make.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use