PHPackages                             servicestack/client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. servicestack/client

ActiveLibrary[API Development](/categories/api)

servicestack/client
===================

PHP ServiceStack Client

1.0.9(1y ago)04.2k↓29.4%BSD-3-Clause-AttributionPHPPHP &gt;=8.2.0

Since Oct 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ServiceStack/servicestack-php)[ Packagist](https://packagist.org/packages/servicestack/client)[ RSS](/packages/servicestack-client/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (11)Used By (0)

[![ServiceStack and PHP Banner](https://camo.githubusercontent.com/bac7974a07207516da223a4ef06cbdec9d27499deecf961ad7156c115b286701/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f7068702d7265666572656e63652e706e67)](https://camo.githubusercontent.com/bac7974a07207516da223a4ef06cbdec9d27499deecf961ad7156c115b286701/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f7068702d7265666572656e63652e706e67)

ServiceStack's **Add ServiceStack Reference** feature allows clients to generate Native Types from directly within PhpStorm using [ServiceStack IntelliJ Plugin](https://plugins.jetbrains.com/plugin/7749-servicestack/) - providing a simple way to give clients typed access to your ServiceStack Services.

[![](https://camo.githubusercontent.com/be12f80f678e659bb9695583ef483170d07b2ceac1ca8b1c7f6a1e7800404877/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f707974686f6e2d6164642d73657276696365737461636b2d7265666572656e63652d796f75747562652d73706c6173682e706e67)](https://youtu.be/WjbhfH45i5k)

> YouTube: [youtu.be/WjbhfH45i5k](https://youtu.be/WjbhfH45i5k)

### First class development experience

[](#first-class-development-experience)

[PHP](https://www.php.net) is one of the worlds most popular programming languages thanks to its ease of use, platform independence, large standard library, flexibility and fast development experience which sees it excels as a popular language for web development and for development of popular CMS products like WordPress, Drupal and Joomla thanks to its flexibility, embeddability and ease of customization.

To maximize the experience for calling ServiceStack APIs within these environments ServiceStack now supports PHP as a 1st class Add ServiceStack Reference supported language which gives PHP developers an end-to-end typed API for consuming ServiceStack APIs, complete with IDE integration in [PhpStorm](https://www.jetbrains.com/phpstorm/) as well as [built-in support in x dotnet tool](https://docs.servicestack.net/dotnet-tool#addupdate-servicestack-references)to generate Typed and annotated PHP DTOs for a remote ServiceStack instance from a single command-line.

### Ideal idiomatic Typed Message-based API

[](#ideal-idiomatic-typed-message-based-api)

To maximize the utility of PHP DTOs and enable richer tooling support and greater development experience, PHP DTOs are generated as Typed [JsonSerializable](https://www.php.net/manual/en/class.jsonserializable.php) classes with [promoted constructors](https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion)and annotated with [PHPDoc Types](https://phpstan.org/writing-php-code/phpdoc-types) - that's invaluable when scaling large PHP code-bases and greatly improves discoverability of a remote API. DTOs are also enriched with interface markers and Annotations which enables its optimal end-to-end typed API:

The PHP DTOs and `JsonServiceClient` library follow [PHP naming conventions](https://infinum.com/handbook/wordpress/coding-standards/php-coding-standards/naming)so they'll naturally fit into existing PHP code bases. Here's a sample of [techstacks.io](https://techstacks.io)generated PHP DTOs containing string and int Enums, an example AutoQuery and a standard Request &amp; Response DTO showcasing the rich typing annotations and naming conventions used:

```
enum TechnologyTier : string
{
    case ProgrammingLanguage = 'ProgrammingLanguage';
    case Client = 'Client';
    case Http = 'Http';
    case Server = 'Server';
    case Data = 'Data';
    case SoftwareInfrastructure = 'SoftwareInfrastructure';
    case OperatingSystem = 'OperatingSystem';
    case HardwareInfrastructure = 'HardwareInfrastructure';
    case ThirdPartyServices = 'ThirdPartyServices';
}

enum Frequency : int
{
    case Daily = 1;
    case Weekly = 7;
    case Monthly = 30;
    case Quarterly = 90;
}

// @Route("/technology/search")
#[Returns('QueryResponse')]
/**
 * @template QueryDb of Technology
 * @template QueryDb1 of TechnologyView
 */
class FindTechnologies extends QueryDb implements IReturn, IGet, JsonSerializable
{
    public function __construct(
        /** @var array|null */
        public ?array $ids=null,
        /** @var string|null */
        public ?string $name=null,
        /** @var string|null */
        public ?string $vendorName=null,
        /** @var string|null */
        public ?string $nameContains=null,
        /** @var string|null */
        public ?string $vendorNameContains=null,
        /** @var string|null */
        public ?string $descriptionContains=null
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        parent::fromMap($o);
        if (isset($o['ids'])) $this->ids = JsonConverters::fromArray('int', $o['ids']);
        if (isset($o['name'])) $this->name = $o['name'];
        if (isset($o['vendorName'])) $this->vendorName = $o['vendorName'];
        if (isset($o['nameContains'])) $this->nameContains = $o['nameContains'];
        if (isset($o['vendorNameContains'])) $this->vendorNameContains = $o['vendorNameContains'];
        if (isset($o['descriptionContains'])) $this->descriptionContains = $o['descriptionContains'];
    }

    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = parent::jsonSerialize();
        if (isset($this->ids)) $o['ids'] = JsonConverters::toArray('int', $this->ids);
        if (isset($this->name)) $o['name'] = $this->name;
        if (isset($this->vendorName)) $o['vendorName'] = $this->vendorName;
        if (isset($this->nameContains)) $o['nameContains'] = $this->nameContains;
        if (isset($this->vendorNameContains)) $o['vendorNameContains'] = $this->vendorNameContains;
        if (isset($this->descriptionContains)) $o['descriptionContains'] = $this->descriptionContains;
        return empty($o) ? new class(){} : $o;
    }
    public function getTypeName(): string { return 'FindTechnologies'; }
    public function getMethod(): string { return 'GET'; }
    public function createResponse(): mixed { return QueryResponse::create(genericArgs:['TechnologyView']); }
}

// @Route("/orgs/{Id}", "DELETE")
class DeleteOrganization implements IReturnVoid, IDelete, JsonSerializable
{
    public function __construct(
        /** @var int */
        public int $id=0
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        if (isset($o['id'])) $this->id = $o['id'];
    }

    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = [];
        if (isset($this->id)) $o['id'] = $this->id;
        return empty($o) ? new class(){} : $o;
    }
    public function getTypeName(): string { return 'DeleteOrganization'; }
    public function getMethod(): string { return 'DELETE'; }
    public function createResponse(): void {}
}
```

The smart PHP `JsonServiceClient` available in the [servicestack/client](https://packagist.org/packages/servicestack/client)packagist package enables the same productive, typed API development experience available in our other 1st-class supported client platforms.

Using promoted constructors enables DTOs to be populated using a single constructor expression utilizing named parameters which together with the generic `JsonServiceClient` enables end-to-end typed API Requests in a single LOC:

```
use ServiceStack\JsonServiceClient;
use dtos\Hello;

$client = new JsonServiceClient("https://test.servicestack.net");

/** @var HelloResponse $response */
$response = client->get(new Hello(name:"World"));
```

> The `HelloResponse` optional type hint doesn't change runtime behavior but enables static analysis tools and IDEs like PyCharm to provide rich intelli-sense and development time feedback.

Installation
------------

[](#installation)

Ensure you have [PHP](https://www.php.net/manual/en/install.php) and [Composer](https://getcomposer.org/doc/00-intro.md) installed.

The only requirements for PHP apps to perform typed API Requests are the generated PHP DTOs and the generic `JsonServiceClient`which can be installed in Composer projects with:

```
$ composer require servicestack/client
```

Or by adding the package to your `composer.json` then installing the dependencies:

```
{
  "require": {
    "servicestack/client": "^1.0"
  }
}
```

### PhpStorm ServiceStack Plugin

[](#phpstorm-servicestack-plugin)

PHP developers of [PhpStorm](https://www.jetbrains.com/phpstorm/) can get a simplified development experience for consuming ServiceStack Services by installing the [ServiceStack Plugin](https://plugins.jetbrains.com/plugin/7749-servicestack) from the JetBrains Marketplace:

[![](https://camo.githubusercontent.com/f122cfbf1f41b0165fa1e6172426cb1698c29da30767c256d6006a25b3808ae3/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f7079636861726d2d73657276696365737461636b2d706c7567696e2e706e67)](https://plugins.jetbrains.com/plugin/7749-servicestack)

Where you'll be able to right-click on a directory and click on **ServiceStack Reference** on the context menu:

[![](https://camo.githubusercontent.com/0b80ec18b6469bd214cb895be424aff0238557d07996681e370f5ef1ac4745bb/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f7079636861726d2d6164642d73657276696365737461636b2d7265666572656e63652e706e67)](https://camo.githubusercontent.com/0b80ec18b6469bd214cb895be424aff0238557d07996681e370f5ef1ac4745bb/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f7079636861726d2d6164642d73657276696365737461636b2d7265666572656e63652e706e67)

To launch the **Add PHP ServiceStack Reference** dialog where you can enter the remote URL of the ServiceStack endpoint you wish to call to generate the Typed PHP DTOs for all APIs which by default will saved to `dtos.php`:

[![](https://camo.githubusercontent.com/c2f103ebb3c4e1bdddae374fad7584ba96f327073920b0c695af5ac84c835a31/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f7079636861726d2d6164642d73657276696365737461636b2d7265666572656e63652d6469616c6f672e706e67)](https://camo.githubusercontent.com/c2f103ebb3c4e1bdddae374fad7584ba96f327073920b0c695af5ac84c835a31/68747470733a2f2f646f63732e73657276696365737461636b2e6e65742f696d672f70616765732f73657276696365737461636b2d7265666572656e63652f7079636861726d2d6164642d73657276696365737461636b2d7265666572656e63652d6469616c6f672e706e67)

Then just import the DTOs and `JsonServiceClient` to be able to consume any of the remote ServiceStack APIs:

```
