Wednesday, March 27, 2024

Sample PHP code for using Google Gemini API


Find below the sample PHP code that explains the use of Google Gemini API to create as web app as explained in this video.

 <?php

//Very Simple php code to explain about using Google Gemini API

$YOUR_API_KEY=""; //Generate API Key at Google AI studio and use it here.

$prompt="tell latest Tech news";

$json_data = '{

      "contents": [{

        "parts":[{

          "text":"'.$prompt.'"}]}]}';

$url="https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$YOUR_API_KEY";

        

$response=run_curl($url,$json_data);

echo ($response);

 

function run_curl($url, $json_data)

{

  $ch = curl_init($url);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);

curl_close($ch);

return $response;

}

      ?>




No comments:

Search This Blog