Publish and Subscribe

With Dapr, you can publish anything, including cloud events. The SDK contains a simple cloud event implementation, but you can also just pass an array that conforms to the cloud event spec or use another library.

<?php
$app->post('/publish', function(\Dapr\Client\DaprClient $daprClient) {
    $daprClient->publishEvent(pubsubName: 'pubsub', topicName: 'my-topic', data: ['something' => 'happened']);
});

For more information about publish/subscribe, check out the howto.

Data content type

The PHP SDK allows setting the data content type either when constructing a custom cloud event, or when publishing raw data.

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#f92672">&lt;?</span><span style="color:#a6e22e">php</span>
</span></span><span style="display:flex;"><span>$event <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">\Dapr\PubSub\CloudEvent</span>();
</span></span><span style="display:flex;"><span>$event<span style="color:#f92672">-&gt;</span><span style="color:#a6e22e">data</span> <span style="color:#f92672">=</span> $xml;
</span></span><span style="display:flex;"><span>$event<span style="color:#f92672">-&gt;</span><span style="color:#a6e22e">data_content_type</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;application/xml&#39;</span>;
</span></span></code></pre></div>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#f92672">&lt;?</span><span style="color:#a6e22e">php</span>
</span></span><span style="display:flex;"><span><span style="color:#e6db74">/**
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> * @var \Dapr\Client\DaprClient $daprClient 
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> */</span>
</span></span><span style="display:flex;"><span>$daprClient<span style="color:#f92672">-&gt;</span><span style="color:#a6e22e">publishEvent</span>(<span style="color:#a6e22e">pubsubName</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;pubsub&#39;</span>, <span style="color:#a6e22e">topicName</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;my-topic&#39;</span>, <span style="color:#a6e22e">data</span><span style="color:#f92672">:</span> $raw_data, <span style="color:#a6e22e">contentType</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;application/octet-stream&#39;</span>);
</span></span></code></pre></div><div class="alert alert-warning" role="alert">
<h4 class="alert-heading">Binary data</h4>
<pre><code>Only &lt;code&gt;application/octet-steam&lt;/code&gt; is supported for binary data.
</code></pre>
</div>

Receiving cloud events

In your subscription handler, you can have the DI Container inject either a Dapr\PubSub\CloudEvent or an array into your controller. The former does some validation to ensure you have a proper event. If you need direct access to the data, or the events do not conform to the spec, use an array.