DEV Community

Shimanta Das
Shimanta Das

Posted on

How to Integrate Gemini API into Your PHP Projects Using Curl

Learn how to seamlessly integrate the powerful Gemini Text API into your PHP applications using the versatile Curl library. This comprehensive guide covers step-by-step instructions, code examples, and best practices to help you effectively leverage Gemini's advanced text capabilities. Unlock the potential of AI-driven text processing and enhance your PHP projects today.

Gemini has many options to work with, here I will introduce about how you can use Gemini Text generation API with your PHP project. You can use this script with various platforms such as WordPress, Laravel, CakePHP, CodeIgniter etc.

Let's see the below code ...

<?php
try {

    $apiKey = 'enter-you-gemini-api-key';
    $apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent';

    $message = "List top PHP frameworks based on PHP. What are their advantages.";


    $data = json_encode([
        'contents' => [
            [
                'parts' => [
                    [
                        'text' => "$message"
                    ]
                ]
            ]
        ]
    ]);

    $ch = curl_init($apiUrl . '?key=' . $apiKey);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json'
    ]);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $response = curl_exec($ch);
    $responseArray = json_decode($response, true);

    if (isset($responseArray['candidates'][0]['content']['parts'][0]['text'])) {
        $text = $responseArray['candidates'][0]['content']['parts'][0]['text'];
        echo $text;
    } else {
        echo "error";
    }


    curl_close($ch);


} catch (Exception $e) {
    echo $e->getMessage();
}
?>

Enter fullscreen mode Exit fullscreen mode

Output

Image description

Top comments (4)

Collapse
 
clarkedev profile image
Will Clarke

Hi, what use case would this have on a website? I can't come up with an actual valid use case, especially for a WordPress site. Interested on where you think this could be used.

Collapse
 
shimanta_microcodes profile image
Shimanta Das • Edited

Hi! Clarke, see in this 2024-25 era one of the fastest growing technology is generative A.I. Sometime when you building website for a client that time, you need A.I for several works like data validation, text generation, context understanding etc. Google Gemini A.I api is one of the best generative A.I tool for developers and business owners.
In short I would say, some time you need chatgpt help inside your application, ln that time you can use it as alternative.

Sorry! From my side for late reply 😊

Collapse
 
wpcoder profile image
Muhammad Bilal

does it free or paid api like chatgpt?

Thread Thread
 
shimanta_microcodes profile image
Shimanta Das

for text generation it's free right now. you can try.