PHP – How to use the ChatGPT API in 4 easy steps

Step 1 – Generate a bearer token

Login and then create an application from the My account menu.

Generate a bearer token from your new application.

Step 2 – Start a new thread with ChatGPT

You may select an ai_version_code. For example : gpt-4.

Send an http POST request to create a new thread.

<?php
$bearer = '<<your_token_here>>';

// CREATE THREAD
// -------------
$url = "https://app.ai-client.com/api/v1/threads";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = [
    'ai_version_code' => 'gpt-4'
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$thread = json_decode($response)->data;
curl_close($curl);

print_r($thread);

This will return :

stdClass Object
(
    [guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
    [ai_code] => chatgpt
    [ai_version_code] => gpt-4
    [title] => New thread with gpt-3.5-turbo
    [created_at] => 2023-06-14T20:54:48.000000Z
    [updated_at] => 2023-06-14T20:54:48.000000Z
)

Step 3 – Create a thread entry to send a message

You may use the thread object to create a new entry with a POST request.

$url = "https://app.ai-client.com/api/v1/threads/" . $thread->guid . "/entry";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = ['type' => 'request', 'message' => 'Hello ChatGPT. Tell me a joke!'];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntry = json_decode($response)->data;
curl_close($curl);

print_r($threadEntry);

This will return :

stdClass Object
(
    [guid] => 2693abb8-27e3-488d-a9df-0648f6e96b88
    [thread_guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
    [type] => request
    [error] =>
    [content] => stdClass Object
        (
            [role] => user
            [content] => Hello ChatGPT. Tell me a joke!
        )

    [waiting_response] => 1
    [credits] => 0
    [created_at] => 2023-06-14T20:54:48.000000Z
)

Step 4 – Wait for a response from ChatGPT

You must now wait for the AI’s response. The “waiting_response” property of your thread entry informs you of ChatGPT’s response.

You may use the thread object to get all the thread entries. Send a GET request. You will see your thread entry of type “request” and the ChatGPT thread entry of type “response“.

$url = "https://app.ai-client.com/api/v1/threads/" . $thread->guid . "/entries";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntries = json_decode($response)->data;
curl_close($curl);

print_r($threadEntries);

This will return :

Array
(
    [0] => stdClass Object
        (
            [guid] => 2693abb8-27e3-488d-a9df-0648f6e96b88
            [thread_guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
            [type] => request
            [error] =>
            [content] => stdClass Object
                (
                    [role] => user
                    [content] => Hello ChatGPT. Tell me a joke!
                )

            [waiting_response] =>
            [credits] => 0
            [created_at] => 2023-06-14T20:54:48.000000Z
        )

    [1] => stdClass Object
        (
            [guid] => 176df6a4-29d1-4515-94ba-63e1c9bb4295
            [thread_guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
            [type] => response
            [error] =>
            [content] => Array
                (
                    [0] => stdClass Object
                        (
                            [message] => stdClass Object
                                (
                                    [role] => assistant
                                    [content] => Sure, here's a joke for you: Why don't scientists trust atoms? Because they make up everything.
                                )

                            [finish_reason] => stop
                            [index] => 0
                        )

                )

            [waiting_response] =>
            [credits] => 2
            [created_at] => 2023-06-14T20:54:50.000000Z
        )

)

Full code example

<?php
$bearer = '<<your_token_here>>';

// CREATE THREAD
// -------------
$url = "https://app.ai-client.com/api/v1/threads";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = [
    'ai_version_code' => 'gpt-4'
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$thread = json_decode($response)->data;
curl_close($curl);

print_r($thread);

/*
stdClass Object
(
    [guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
    [ai_code] => chatgpt
    [ai_version_code] => gpt-4
    [title] => New thread with gpt-4
    [created_at] => 2023-06-14T20:54:48.000000Z
    [updated_at] => 2023-06-14T20:54:48.000000Z
)
*/

// CREATE THREAD ENTRY OF TYPE request
// -----------------------------------
$url = "https://app.ai-client.com/api/v1/threads/" . $thread->guid . "/entry";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = ['type' => 'request', 'message' => 'Hello ChatGPT. Tell me a joke!'];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntry = json_decode($response)->data;
curl_close($curl);

print_r($threadEntry);

/*
stdClass Object
(
    [guid] => 2693abb8-27e3-488d-a9df-0648f6e96b88
    [thread_guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
    [type] => request
    [error] =>
    [content] => stdClass Object
        (
            [role] => user
            [content] => Hello ChatGPT. Tell me a joke!
        )

    [waiting_response] => 1
    [credits] => 0
    [created_at] => 2023-06-14T20:54:48.000000Z
)
*/

// WAIT FOR 20 SECONDS
// -------------------
sleep(20);

// GET THREAD ENTRIES
// ------------------
$url = "https://app.ai-client.com/api/v1/threads/" . $thread->guid . "/entries";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntries = json_decode($response)->data;
curl_close($curl);

print_r($threadEntries);

/*
Array
(
    [0] => stdClass Object
        (
            [guid] => 2693abb8-27e3-488d-a9df-0648f6e96b88
            [thread_guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
            [type] => request
            [error] =>
            [content] => stdClass Object
                (
                    [role] => user
                    [content] => Hello ChatGPT. Tell me a joke!
                )

            [waiting_response] =>
            [credits] => 0
            [created_at] => 2023-06-14T20:54:48.000000Z
        )

    [1] => stdClass Object
        (
            [guid] => 176df6a4-29d1-4515-94ba-63e1c9bb4295
            [thread_guid] => 338bfe26-550c-4720-8cba-d95facd9d20f
            [type] => response
            [error] =>
            [content] => Array
                (
                    [0] => stdClass Object
                        (
                            [message] => stdClass Object
                                (
                                    [role] => assistant
                                    [content] => Sure, here's a joke for you: Why don't scientists trust atoms? Because they make up everything.
                                )

                            [finish_reason] => stop
                            [index] => 0
                        )

                )

            [waiting_response] =>
            [credits] => 2
            [created_at] => 2023-06-14T20:54:50.000000Z
        )

)
*/

See our Swagger API documentation for more information.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *