Let’s create an example in PHP and generate awesome contents with the Learnitive AI.
Endpoint:
https://www.learnitive.com/api/v1/contents
Learn more about the Endpoints.
PHP Code
<?php
$context = "BMW car sell";
$keywords = ["electric"];
$max_tokens = "256";
$temperature = ".65";
$API_KEY = "your-api-key";
$model = "balanced";
$endpoint = "https://www.learnitive.com/api/v1/contents";
$request_body = [
"input" => $context,
"mode" => $mode,
"keywords" => $keywords,
"max_tokens" => $max_tokens,
"temperature" => $temperature,
];
$postfields = json_encode($request_body);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'api-key:' . $_API_KEY
],
]);
$response = curl_exec($curl);
var_dump($response);
curl_close($curl);
?>
HTML and JS code
Let’s now print the output as a JS object.
<!DOCTYPE html>
<html>
<head>
API Test
</head>
<body>
<p>API test with JS</p>
<script>
var response = JSON.parse(<?=json_encode($response);?>);
console.log(response.message[0].text);
</script>
</body>
</html>
Ouptuts
Let’s now check the PHP and JS object output.
PHP Output
string(1344)
"{"
success ":true,"
status ":"
success ","
message ":[{"
index ":0,"
text ":" BMW has recently announced its plans to introduce a new electric car model for those..","
id ":"420 eb4c5 - ca68 - 4326 - a386 - 888 c9ac2380d "
}]
}"
JS Output
BMW has recently announced its plans to introduce a new electric car model for those..