php - PHP 致命错误:无法重新声明 Google Dialogflow PHP API 的问题
问题描述
Google Dialogflow 有问题。我不断收到错误消息:
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
有人可以提供帮助吗?就上下文而言,我正在使用第三方 SMS API,因此我可以构建一个 SMS 聊天机器人。我已经清理了其余的代码,但是我所做的每个测试都会出现错误。不知道为什么我不能在这里重新声明图书馆。
<?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");
?>
解决方案
错误消息包含有用的信息。
该函数detect_intent_texts
已在Google\Cloud\Samples\Dialogflow
命名空间中声明/var/www/fixnode-website/php-docs-samples/dialogflow/src/detect_intent_texts.php
在您的脚本中,您detect_intent_texts
在Google\Cloud\Samples\Dialogflow
命名空间中重新声明。您不应该在诸如此类的供应商包中声明的名称空间中声明函数。
您应该在脚本中声明一个不同的命名空间。
如果您的项目未出售,您可以删除命名空间声明。
<?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;
推荐阅读
- boost-test - 观察测试失败消息
- reporting-services - 如何获得累积总和 SSRS 报告?
- azure - 如何在 Azure API 管理 Python SDK 中使用筛选器
- css - 我如何停止 prepos 来创建除 main.scss 之外的某些已编译的 scss
- android - React Native - 用于将我的应用程序提交到 Google Play 的未优化 APK
- ios - xib 显示空白框,即使我们在 Xcode 中有很多视图和其他设计
- vim - 摆脱 vim 的“netrw”插件
- c# - 如何检查gridviewrow中是否选中了复选框
- wpf - 用于填充 ListBox 的 LINQ to SQL 查询返回错误结果
- python - 为什么我得到 DatasetV1Adapter 返回类型而不是 tf.data.Dataset.from_tensor_slices(X) 的 TensorSliceDataset