PHP code example of telnyx / telnyx-php

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

    

telnyx / telnyx-php example snippets




use Telnyx\Client;

$client = new Client(apiKey: getenv('TELNYX_API_KEY') ?: 'My API Key');

$response = $client->calls->dial(
  connectionID: 'conn12345',
  from: '+15557654321',
  to: '+15551234567',
  webhookURL: 'https://your-webhook.url/events',
);

var_dump($response->data);



use Telnyx\Client;

$client = new Client(apiKey: getenv('TELNYX_API_KEY') ?: 'My API Key');

$page = $client->accessIPAddress->list(pageNumber: 1, pageSize: 50);

var_dump($page);

// fetch items from the current page
foreach ($page->getItems() as $item) {
  var_dump($item->id);
}
// make additional network requests to fetch items from all pages, including and after the current page
foreach ($page->pagingEachItem() as $item) {
  var_dump($item->id);
}



use Telnyx\Core\Exceptions\APIConnectionException;
use Telnyx\Core\Exceptions\RateLimitException;
use Telnyx\Core\Exceptions\APIStatusException;

try {
  $numberOrder = $client->numberOrders->create();
} catch (APIConnectionException $e) {
  echo "The server could not be reached", PHP_EOL;
  var_dump($e->getPrevious());
} catch (RateLimitException $e) {
  echo "A 429 status code was received; we should back off a bit.", PHP_EOL;
} catch (APIStatusException $e) {
  echo "Another non-200-range status code was received", PHP_EOL;
  echo $e->getMessage();
}



use Telnyx\Client;

// Configure the default for all requests:
$client = new Client(requestOptions: ['maxRetries' => 0]);

// Or, configure per-request:
$result = $client->numberOrders->create(
  phoneNumbers: [['phoneNumber' => '+15558675309']],
  requestOptions: ['maxRetries' => 5],
);



use Telnyx\Core\FileParam;

// Pass a string with filename and content type:
$contents = file_get_contents('/path/to/file');
// Pass a string with filename and content type:
$response = $client->ai->audio->transcribe(
  file: FileParam::fromString($contents, filename: '/path/to/file', contentType: '…'),
);

// Pass in only a string (where applicable)
$response = $client->ai->audio->transcribe(file: '…');

// Pass an open resource:
$fd = fopen('/path/to/file', 'r');
try {
  $response = $client->ai->audio->transcribe(
    file: FileParam::fromResource($fd, filename: '/path/to/file', contentType: '…'),
  );
} finally {
  fclose($fd);
}



$numberOrder = $client->numberOrders->create(
  phoneNumbers: [['phoneNumber' => '+15558675309']],
  requestOptions: [
    'extraQueryParams' => ['my_query_parameter' => 'value'],
    'extraBodyParams' => ['my_body_parameter' => 'value'],
    'extraHeaders' => ['my-header' => 'value'],
  ],
);



$response = $client->request(
  method: "post",
  path: '/undocumented/endpoint',
  query: ['dog' => 'woof'],
  headers: ['useful-header' => 'interesting-value'],
  body: ['hello' => 'world']
);

composer