# Discourse SSO and API Helper for PHP

**URL:** https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172
**Category:** Extras
**Created:** [2016年十月6日 10:42 UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172 "2016-10-06T10:42:40Z")
**Posts on this page:** 7
**Page:** 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: [2016年十月6日 10:42 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);

```

---

<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: [2016年十月6日 10:46 UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172/2 "2016-10-06T10:46:22Z")

</div>

Since I committed in other task I didn’t tested the code well and unable to give detailed documentation with usage examples.

I will release next version soon with more options, documentation and examples.

---

<div class="post-metadata">

### Author: ![erlend\_sh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erlend_sh/32/119475_2.png) [@erlend\_sh](https://meta.discourse.org/u/erlend_sh)
#### Post date: [2016年十月7日 11:47 UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172/3 "2016-10-07T11:47:52Z")

</div>

I haven’t looked closely at this but it sounds like there might be some overlap with @michaeld’s PHP API client for Discourse:

[https://github.com/communiteq/discourse-api-php](https://github.com/communiteq/discourse-api-php)

---

<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: [2016年十月7日 12:06 UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172/4 "2016-10-07T12:06:41Z")

</div>

I already aware of this existing package. But I didn’t used it. I used famous [**GuzzleHttp**](https://github.com/guzzle/guzzle) (they used php native curl) and **composer**. Also in mine it have SSO option too.

After all I created this package because of I am also creating [laravel package](https://github.com/vinkas0/discourse-laravel) (which depends it) for easy implementation with laravel features like eloquent model. Yes few laravel packages also already exist for discourse. But I have something different in my mind for development 🙂

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [2016年十月7日 16:05 UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172/5 "2016-10-07T16:05:43Z")

</div>

> [@vinothkannans](#):
>
> I already aware of this existing package. But I didn’t used it.

Too bad you decided to create another, instead of just contributing to what already existed, and making it better.

---

<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: [2016年十月7日 16:14 UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172/6 "2016-10-07T16:14:58Z")

</div>

Sorry @michaeld. Coding structure which I wanted and created is totally different from your existing package. That’s why I created it as new one.

In other words I created this package for my own purpose. Then I decided to open source it.

---

<div class="post-metadata">

### Author: ![pnoeric](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pnoeric/32/177144_2.png) [@pnoeric](https://meta.discourse.org/u/pnoeric)
#### Post date: [2020年三月23日 15:48 UTC](https://meta.discourse.org/t/discourse-sso-and-api-helper-for-php/51172/7 "2020-03-23T15:48:45Z")

</div>

给刚看到这个话题的朋友（现在是2020年3月……来自居家新冠隔离的问候）——

上面提到的来自 @michaeld 的库似乎仅用于与 API 交互，与 SSO 并无实际关联。

我一直在试用 @vinothkannans 在 GitHub 上分享的库，但正如他自己所述，他尚未进行大量测试。事实上，我发现该库存在不少漏洞，尚未达到可投入生产使用的成熟度。

我找到了这个库，目前运行良好：

[https://github.com/cviebrock/discourse-php](https://github.com/cviebrock/discourse-php)
