- PHP Client Library for Dialogflow API v2
- To begin,lets clone this repository
git clone https://github.com/sumithemmadi/dialogflow-php.git
cd dialogflow-php
- Then install the preferred dependencies for Dialogflow.
composer install
You should do the following before reading this guide:
- First create a project on Google Cloud Console then create a service account and download the private key file.
- Create Agent in Dialogflow and get the project ID from settings.
for more details click here.
- There you can see Your Project ID at GOOGLE PROJECT in GENERAL settings
Create a service account:
-
In the Cloud Console, go to the Create service account page.
Go to Create service account - Select a project.
-
In the Service account name field, enter a name. The Cloud Console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Click the Select a role field.
Under Quick access, click Basic, then click Owner.
Note: The Role field affects which resources your service account can access in your project. You can revoke these roles or grant additional roles later. In production environments, do not grant the Owner, Editor, or Viewer roles. Instead, grant a predefined role or custom role that meets your needs.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
Create a service account key:
- In the Cloud Console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
<?php
namespace Google\Cloud\Samples\Dialogflow;
require __DIR__ . "/vendor/autoload.php";
// Save Google Account Credentials json file in root directory
// make sure that Google Account Credentials JSON file and this file are in same directory.
$google_application_credentials = "[GOOGLE ACCOUNT_CREDENTIALS_JSON_FILE]";
// PROJECT ID
// Please Change "[ENTER_PROJECT_ID]" with you dialogflow project ID
$projectId = "[ENTER_PROJECT_ID]";
$languageCode = 'en-US';
$sessionId = "123456789";
// Get response from text input.
$text = "hi";
$queryResponse = get_response_from_text($google_application_credentials,$projectId,$sessionId,$languageCode,$text);
echo $queryResponse;
?>
<?php
namespace Google\Cloud\Samples\Dialogflow;
require __DIR__ . "/vendor/autoload.php";
// Save Google Account Credentials json file in root directory
// make sure that Google Account Credentials JSON file and this file are in same directory.
$google_application_credentials = "[GOOGLE ACCOUNT_CREDENTIALS_JSON_FILE]";
// PROJECT ID
// Please Change "[ENTER_PROJECT_ID]" with you dialogflow project ID
$projectId = "[ENTER_PROJECT_ID]";
$languageCode = 'en-US';
$sessionId = "123456789";
// Get Response from audio input.
$path = "test/hello.wav";
$audioResponse = get_response_from_audio($google_application_credentials,$projectId,$path, $sessionId, $languageCode);
echo $audioResponse;
?>
{
"responseId": "f7f810a1-0043-4f85-ac6c-241705aa6b8a-94f60986",
"queryResult": {
"queryText": "my name is sumith",
"languageCode": "en",
"parameters": {
"person": {
"name": "sumith"
}
},
"allRequiredParamsPresent": true,
"fulfillmentText": "Sorry, I was not able to understand your intention . I'm trying to get better at this thing.",
"fulfillmentMessages": [
{
"text": {
"text": [
"Sorry, I was not able to understand your intention . I'm trying to get better at this thing."
]
}
}
],
"intent": {
"name": "projects\/sumith-rwup\/agent\/intents\/f95d3add-52fb-4119-87f0-e1717181173d",
"displayName": "user.name"
},
"intentDetectionConfidence": 1
}
}
-
Create a nginx server
-
Run below command to generate nginx.conf file for your server.
~$ php nginx-conf.php
- Now nginx.conf will be created in you local directory.
- move the nginx.conf file to /etc/nginx/.
- now start the nginx server (Default port will be 80)
- Then start php-cgi on port 9000
~$ php-cgi -b 127.0.0.1:9000 & nginx
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
Copyright 2021 sumithemmadi
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.