# Discourse SSO and API Helper for PHP

**URL:** <https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172>\
**Category:** Extras\
**Created:** [6 באוקטובר,‏ 2016,‏ 10:42am UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172 "2016-10-06T10:42:40Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)\
**Post date:** [6 באוקטובר,‏ 2016,‏ 10:42am UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172/1 "2016-10-06T10:42:40Z")

</div>

### Installation

The package is registered at Packagist as [vinkas/discourse-php](https://packagist.org/packages/vinkas/discourse-php) and can be installed using composer:

```plaintext
composer require vinkas/discourse-php

```

### Usage

```php
$discourse = new Vinkas\Discourse\PHP\Client('discouse.example.com', true); // set true if ssl enabled

```

#### API

```php
$api = $discourse->api('API_KEY', 'API_USERNAME');
$api->topics()->create('TITLE', 'CONTENT', 'CATEGORY_SLUG')

```

#### SSO

```php
$payload = $_GET['sso'];
$signature = $_GET['sig'];

$sso = $discourse->sso('SECRET', $payload, $signature);

if (!($sso->isValid())) {
    header("HTTP/1.1 403 Forbidden");
    echo("Bad SSO request");
    die();
}

$userParams = array(
    'external_id' => 'USER_ID',
    'email' => 'EMAIL_ADDRESS',
    'username' => 'USERNAME', // optional
    'name' => 'FULL_NAME' // optional
    // for more available fields https://meta.discourse.org/t/official-single-sign-on-for-discourse/13045
);

$url = $sso->getResponseUrl($userParams)
header('Location: ' . $url);
exit(0);

```

---

_[View the full topic](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172)._
