PHP code example of google / cloud-natural-language

1. Go to this page and download the library: Download google/cloud-natural-language library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

google / cloud-natural-language example snippets


use Google\ApiCore\ApiException;
use Google\Cloud\Language\V2\AnalyzeSentimentRequest;
use Google\Cloud\Language\V2\AnalyzeSentimentResponse;
use Google\Cloud\Language\V2\Client\LanguageServiceClient;
use Google\Cloud\Language\V2\Document;

// Create a client.
$languageServiceClient = new LanguageServiceClient();

// Prepare the request message.
$document = new Document();
$request = (new AnalyzeSentimentRequest())
    ->setDocument($document);

// Call the API and handle any network failures.
try {
    /** @var AnalyzeSentimentResponse $response */
    $response = $languageServiceClient->analyzeSentiment($request);
    printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
    printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}