定制 telnyx/telnyx-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

telnyx/telnyx-php

Composer 安装命令:

composer require telnyx/telnyx-php

包简介

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

README 文档

README

The Telnyx PHP library provides convenient access to the Telnyx REST API from any PHP 8.1.0+ application.

It is generated with Stainless.

Documentation

Installation

composer require "telnyx/telnyx-php 7.92.0"

Usage

This library uses named parameters to specify optional arguments. Parameters with a default value must be set by name.

<?php

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);

Value Objects

It is recommended to use the static with constructor AzureVoiceSettings::with(type: 'azure', ...) and named parameters to initialize value objects.

However, builders are also provided (new AzureVoiceSettings)->withType('azure').

Pagination

List methods in the Telnyx API are paginated.

This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:

<?php

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);
}

Handling errors

When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of Telnyx\Core\Exceptions\APIException will be thrown:

<?php

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();
}

Error codes are as follows:

Cause Error Type
HTTP 400 BadRequestException
HTTP 401 AuthenticationException
HTTP 403 PermissionDeniedException
HTTP 404 NotFoundException
HTTP 409 ConflictException
HTTP 422 UnprocessableEntityException
HTTP 429 RateLimitException
HTTP >= 500 InternalServerException
Other HTTP error APIStatusException
Timeout APITimeoutException
Network error APIConnectionException

Retries

Certain errors will be automatically retried 2 times by default, with a short exponential backoff.

Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default.

You can use the maxRetries option to configure or disable this:

<?php

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],
);

File uploads

Request parameters that correspond to file uploads can be passed as a resource returned by fopen(), a string of file contents, or a FileParam instance.

<?php

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);
}

Advanced concepts

Making custom or undocumented requests

Undocumented properties

You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:

Note: the extra* parameters of the same name overrides the documented parameters.

<?php

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

Undocumented request params

If you want to explicitly send an extra param, you can do so with the extra_query, extra_body, and extra_headers under the request_options: parameter when making a request, as seen in the examples above.

Undocumented endpoints

To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using client.request, like so:

<?php

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

Versioning

This package follows SemVer conventions. As the library is in initial development and has a major version of 0, APIs may change at any time.

This package considers improvements to the (non-runtime) PHPDoc type definitions to be non-breaking changes.

Requirements

PHP 8.1.0 or higher.

Contributing

See the contributing documentation.

telnyx/telnyx-php 适用场景与选型建议

telnyx/telnyx-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 799.67k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2019 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「sms」 「numbers」 「voip」 「telephony」 「whatsapp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 telnyx/telnyx-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 telnyx/telnyx-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 telnyx/telnyx-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 799.67k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 29
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 6
  • Forks: 34
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-07