PHP code example of sean-luis / ibm-php-sdk

1. Go to this page and download the library: Download sean-luis/ibm-php-sdk 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/ */

    

sean-luis / ibm-php-sdk example snippets


use IBMCloud\Helpers\Config;
use IBMCloud\NLU\NaturalLanguageUnderstanding;

// Load IBM Cloud credentials from a YAML file
$credentials = Config::getCredentials('ibm-cloud.yaml');

$authenticator = new IamAuthenticator($credentials['ibm']['api_key']);

$token = $authenticator->getAccessToken();

// Create an instance of NaturalLanguageUnderstanding
$nlu = new NaturalLanguageUnderstanding($token, $url, $version);

// Define the text to analyze
$text = "I am very happy with the result of today's game.";

// Define your feature options for text analysis
$options = [
    'entities' => [
        'sentiment' => true,
        'emotion' => true,
        'limit' => 10,
    ],
    'keywords' => [
        'sentiment' => true,
        'emotion' => true,
        'limit' => 10,
    ],
    'sentiment' => [
        'document' => true,
    ],
    'categories' => [
        'limit' => 5,
    ],
];

// Instantiate NLUFeaturesParams with custom options
$nluFeaturesParams = new NLUFeaturesParams($options);

// Make the request to IBM NLU
$response = $nlu->analyze($text, $features);

// Print the results
foreach ($response['keywords'] as $keyword) {
    echo $keyword['text'].' => Sentiment: '.$keyword['sentiment']['score'].', Emotion: '.$keyword['emotion']['sadness']
    .'<br>';
}

new NaturalLanguageUnderstanding($token, $url, $version)

$nlu->analyze($text, $features)

new NLUFeaturesParams($options)

use IBMCloud\Helpers\Config;
use IBMCloud\COS\CloudObjectStorage;

// Load IBM Cloud credentials from a YAML file
$credentials = Config::getCredentials('ibm-cloud.yaml');

// Sign in and get IBM Cloud token
$authenticator = new IamAuthenticator($credentials['ibm']['api_key']);

$token = $authenticator->getAccessToken();

// Create an instance of CloudObjectStorage
$this->cos = new CloudObjectStorage(
    $token,
    $credentials['cos']['instance_id'],
    $credentials['cos']['url']
);        

// Upload a file to IBM COS
$objectName = 'test.txt';
$body = 'This is a test file.';
$response = $cos->putObject($credentials['cos']['bucket'], $objectName, $body);

// Download a file from IBM COS
$fileName = 'test.txt';
$response = $cos->getObject($bucket, $fileName);

// Print the content of the downloaded file
echo $response;

new CloudObjectStorage($token, $serviceInstanceId, $endpoint, $bucketName)

$cos->putObject($bucketName, $objectName, $body);

$cos->getObject($bucket, $fileName);

$cos->copyObject($sourceBucket, $sourceObject, $destinationBucket, $destinationObject, $contentType = null, $metadata = []);

$cos->deleteObject($bucketName, $objectName);

$cos->listObjects($bucketName, $prefix = null, $delimiter = null);

$cos->createBucket($bucketName, $region);

$cos->deleteBucket($bucketName);

use IBMCloud\Helpers\Config;
use IBMCloud\TTS\TextToSpeech;

// Load IBM Cloud credentials from a YAML file
$credentials = Config::getCredentials('ibm-cloud.yaml');

$token = $authenticator->getAccessToken();

// Create an instance of TextToSpeech
$tts = new TextToSpeech(
    $token,
    $credentials['tts']['url']
);

// Define the necessary parameters
$text = 'This is a test.';

// The VoiceOptionsEnum class contains the supported voice models.
use Sean\IbmPhpSdk\IBMCloud\TTS\Enum\VoiceOptions;
$voice = VoiceOptions::EN_US_ALLISON_V3_VOICE;

// The AcceptOptionsEnum class contains the accepted audio formats.
use Sean\IbmPhpSdk\IBMCloud\TTS\Enum\AcceptOptions;
$accept = AcceptOptions::AUDIO_MP3;

// Make the request to IBM TTS
$audio = $tts->synthesize($text, $accept, $voice);

// Export the audio to a folder
file_put_contents('./path/to/audio.mp3', $audio);

new TextToSpeech($token, $url)

$ttf->synthesize($text, $accept, $voice);

use IBMCloud\Helpers\Config;
use IBMCloud\STT\SpeechToText;

// Load IBM Cloud credentials from a YAML file
$credentials = Config::getCredentials('ibm-cloud.yaml');

$token = $authenticator->getAccessToken();

// Create an instance of TextToSpeech
$sst = new SpeechToText(
    $token,
    $credentials['stt']['url']
);

// Define the necessary parameters
$path = __DIR__.'\audio.mp3';

$params = new SpeechToTextParams([
    'audio' => $path,
    'content_type' => 'audio/mp3',
    'model' => MultimediaModels::SPANISH_CASTILIAN,
    'timestamps' => true,
    'word_alternatives_threshold' => 0.9,
    'keywords' => ['example', 'keyword'],
    'keywords_threshold' => 0.5
]);

// Make the request to IBM STT
$transcript = $sst->recognize($params);

// Display transcribed content based on sent audio.
echo $transcript;

new SpeechToText($token, $url)

$stt->recognize($params);

new SpeechToTextParams([
    'audio' => $path,
    'content_type' => 'audio/mp3',
    'model' => MultimediaModels::SPANISH_CASTILIAN,
    'timestamps' => true,
    'word_alternatives_threshold' => 0.9,
    'keywords' => ['example', 'keyword'],
    'keywords_threshold' => 0.5
])

OK (XX tests, XX assertions)