Search code examples
phpgoogle-apismsdialogflow-esgoogle-api-php-client

PHP Fatal error: Cannot redeclare issue with Google Dialogflow PHP API


Having an issue with Google Dialogflow. I keep getting an error: PHP Fatal error: Cannot redeclare Google\Cloud\Samples\Dialogflow\detect_intent_texts() (previously declared in /var/www/fixnode-website/php-docs-samples/dialogflow/src/detect_intent_texts.php:18) in /var/www/fixnode-website/php-docs-samples/dialogflow/src/detect_intent_texts.php on line 74

Any chance someone can help? For context, I'm using a third party SMS API so I can build an SMS chatbot. I've gotten the rest of the code all cleaned up but every test I do comes up with error's. Not sure why I cannot redeclare the library here.

<?php
// [START dialogflow_detect_intent_text]

use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;
require_once '/var/www/fixnode-website/zang-php/connectors/Sms.php';
require_once __DIR__ . '/../vendor/autoload.php';
/**
 * Returns the result of detect intent with texts as inputs.
 * Using the same `session_id` between requests allows continuation
 * of the conversation.
 */

$SMS = $Body = $_POST['Body'];

function detect_intent_texts($projectId, $texts, $sessionId, $languageCode = 'en-US')
{
    // new session
    $sessionsClient = new SessionsClient();
    $session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid());
    printf('Session path: %s' . PHP_EOL, $session);

    // query for each string in array
    foreach ($texts as $text) {
        // create text input
        $textInput = new TextInput();
        $textInput->setText($text);
        $textInput->setLanguageCode($languageCode);

        // create query input
        $queryInput = new QueryInput();
        $queryInput->setText($textInput);

        // get response and relevant info
        $response = $sessionsClient->detectIntent($session, $queryInput);
        $queryResult = $response->getQueryResult();
        $queryText = $queryResult->getQueryText();
        $intent = $queryResult->getIntent();
        $displayName = $intent->getDisplayName();
        $confidence = $queryResult->getIntentDetectionConfidence();
        $fulfilmentText = $queryResult->getFulfillmentText();

        // output relevant info
        print(PHP_EOL);
        try {
            $sms = Sms::getInstance();
            $sms -> setOptions(array(
            "account_sid"   => $_ENV["ACCOUNT_SID"],
            "auth_token"    => $_ENV["AUTH_TOKEN"],
            ));
            $sentSms = $sms -> sendSms(array(
            'From'          => '647799XXXX',
            'To'            => '416830XXXX',
            'Body'          => $fulfilmentText,
            'AllowMultiple' => "True"
            ));

            echo "<pre>";
            print_r($sentSms->getResponse());
            echo "</pre>";

            }
            catch (ZangException $e){
               echo "<pre>";
               print_r( "Exception message: " . $e -> getMessage() . "<br>");
               print_r( "Exception code: " . $e -> getCode() . "<br>");
               print_r(  $e -> getTrace() );
               echo "</pre>";
            }
   }
   $sessionClient->close();
}

detect_intent_texts("boxwood-ray-226216", "Hi", "12345678");

?>

Solution

  • The error message packs helpful information.

    The function detect_intent_texts is already declared in Google\Cloud\Samples\Dialogflow namespace in /var/www/fixnode-website/php-docs-samples/dialogflow/src/detect_intent_texts.php

    In your script you redeclare detect_intent_texts in Google\Cloud\Samples\Dialogflow namespace. You shouldn't declare functions in namespaces declared in vendored packages such as this one.

    You should declare a different namespace in your script.

    If your project is not vendored you could remove the namespace declaration.

    <?php
    // [START dialogflow_detect_intent_text]
    require_once __DIR__ . '/../vendor/autoload.php';
    require_once '/var/www/fixnode-website/zang-php/connectors/Sms.php';
    
    use Google\Cloud\Dialogflow\V2\SessionsClient;
    use Google\Cloud\Dialogflow\V2\TextInput;
    use Google\Cloud\Dialogflow\V2\QueryInput;