> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fluz.app/llms.txt
> Use this file to discover all available pages before exploring further.

# 現金回饋與聯盟網站的商家資料饋送

> 一個腳本即可取得所有 Fluz 商家、其現金回饋率以及您的推薦連結,可直接列於您的網站上。

如果您經營現金回饋、優惠券或優惠資訊網站,只需這一頁就能列出 Fluz 商家。您只要複製四個值、執行一個腳本,就會得到一個檔案,內含所有商家、其現金回饋率,以及可將新會員記入您名下的連結。

此腳本適用於個人與商業 Fluz 帳戶。每天執行一次即可。

## 您會得到什麼

每個商家一筆資料:

```json theme={null}
{
  "name": "Kohl's",
  "slug": "kohls",
  "cashback": 6,
  "logo_url": "https://storage.googleapis.com/.../kohls-logo.jpg",
  "categories": ["Retail"],
  "link": "https://fluz.app/store/kohls?referred_by=YOURCODE"
}
```

| 欄位 | 說明 |
| :- | :- |
| `name` | 要顯示在您網站上的商家名稱 |
| `slug` | Fluz 為該商家設定的 ID。此 ID 不會改變,因此您可以逐日比對商家。 |
| `cashback` | Fluz 在該商家提供的最高現金回饋率,以百分比表示。`6` 代表最高 6%。 |
| `logo_url` | 商家標誌,或為 `null` |
| `categories` | 購物類別,例如餐飲或旅遊。可能為空。 |
| `link` | 該商家在 fluz.app 上附有您推薦碼的頁面。請將它用作對外連結。 |

## 開始之前

您需要四個值,全部都來自您的 Fluz 帳戶。

| 值 | 取得位置 |
| :- | :- |
| **API Key** | 登入 [fluz.app/for-developers](https://fluz.app/for-developers),建立應用程式並開啟它。 |
| **User ID** | 與 API Key 位於同一頁面 |
| **Account ID** | 與 API Key 位於同一頁面 |
| **推薦碼** | 在 fluz.app 上,開啟 **Maximize** > **Invite a friend**,然後選取 **Copy**。 |

> [!WARNING]
>
> 請在 **fluz.app/for-developers** 建立應用程式。在沙盒(`uni.staging.fluzapp.com`)建立的應用程式其金鑰不同,正式環境 API 會以 `Error verifying API basic token` 拒絕該金鑰。

## 執行資料饋送

> [tab] PHP
>
> 將以下內容儲存為 `fluz-feed.php`,把您的四個值貼到檔案頂端,然後執行 `php fluz-feed.php`。它會在腳本旁寫入 `fluz-feed.json`。需要 PHP 7.4 或更新版本,並啟用 curl 擴充功能。
>
> ```php fluz-feed.php theme={null}
> <?php
> // Fluz merchant feed: every Fluz merchant with its cashback rate and your referral link.
> // Run it once a day, for example from cron. Needs PHP 7.4 or newer with the curl extension.
>
> $API_KEY       = 'YOUR_API_KEY';        // fluz.app/for-developers > your app > API Key
> $USER_ID       = 'YOUR_USER_ID';        // same page
> $ACCOUNT_ID    = 'YOUR_ACCOUNT_ID';     // same page
> $REFERRAL_CODE = 'YOUR_REFERRAL_CODE';  // fluz.app > Maximize > Invite a friend
>
> const FLUZ_API = 'https://transactional-graph.fluzapp.com/api/v1/graphql';
>
> function fluz($auth, $query, $variables = [])
> {
>     $ch = curl_init(FLUZ_API);
>     curl_setopt_array($ch, [
>         CURLOPT_POST           => true,
>         CURLOPT_RETURNTRANSFER => true,
>         CURLOPT_TIMEOUT        => 60,
>         CURLOPT_HTTPHEADER     => ['Authorization: ' . $auth, 'Content-Type: application/json'],
>         CURLOPT_POSTFIELDS     => json_encode(['query' => $query, 'variables' => $variables]),
>     ]);
>     $body   = curl_exec($ch);
>     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
>     if ($body === false) {
>         throw new Exception('Request failed: ' . curl_error($ch));
>     }
>     curl_close($ch);
>     $json = json_decode($body, true);
>     if ($status !== 200 || !empty($json['errors'])) {
>         throw new Exception("Fluz API error (HTTP $status): $body");
>     }
>     return $json['data'];
> }
>
> // 1. Get an access token with your API key.
> $data = fluz('Basic ' . $API_KEY,
>     'mutation ($userId: UUID, $accountId: UUID, $scopes: [ScopeType!]!) {
>        generateUserAccessToken(userId: $userId, accountId: $accountId, scopes: $scopes) { token }
>      }',
>     ['userId' => $USER_ID, 'accountId' => $ACCOUNT_ID, 'scopes' => ['LIST_OFFERS']]
> );
> $token = $data['generateUserAccessToken']['token'];
>
> // 2. Read the catalog 20 merchants at a time until a page comes back empty.
> $query = 'query ($paginate: OffsetInput, $offerTypes: OfferTypesInput) {
>   getMerchants(paginate: $paginate, offerTypes: $offerTypes) {
>     name
>     slug
>     logoUrl
>     categories { name }
>     offers { offerRates { maxUserRewardValue } }
>   }
> }';
>
> $feed = [];
> for ($offset = 0; ; $offset += 20) {
>     $page = fluz('Bearer ' . $token, $query, [
>         'paginate'   => ['limit' => 20, 'offset' => $offset],
>         'offerTypes' => ['giftCardOffer' => true, 'cardLinkedOffer' => false],
>     ])['getMerchants'];
>
>     if (empty($page)) {
>         break;
>     }
>
>     foreach ($page as $m) {
>         // A merchant can have more than one offer. List its best rate.
>         $rate = 0;
>         foreach ($m['offers'] ?? [] as $offer) {
>             foreach ($offer['offerRates'] ?? [] as $r) {
>                 $rate = max($rate, (float) $r['maxUserRewardValue']);
>             }
>         }
>
>         $feed[] = [
>             'name'       => $m['name'],
>             'slug'       => $m['slug'],
>             'cashback'   => $rate,
>             'logo_url'   => $m['logoUrl'],
>             'categories' => array_column($m['categories'] ?? [], 'name'),
>             'link'       => 'https://fluz.app/store/' . $m['slug'] . '?referred_by=' . rawurlencode($REFERRAL_CODE),
>         ];
>     }
> }
>
> // 3. Save the feed. Your site reads this file.
> file_put_contents(__DIR__ . '/fluz-feed.json', json_encode($feed, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
> echo count($feed) . " merchants saved to fluz-feed.json\n";
> ```

> [tab] curl
>
> 您可以在任何語言中使用這兩個請求。兩者都發送到同一個 URL:
>
> ```text theme={null}
> POST https://transactional-graph.fluzapp.com/api/v1/graphql
> ```
>
> **1. 取得存取權杖。** 請原封不動地送出主控台中顯示的 API Key,它已經過編碼。
>
> ```bash theme={null}
> curl -X POST https://transactional-graph.fluzapp.com/api/v1/graphql \
>   -H "Authorization: Basic YOUR_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "query": "mutation ($userId: UUID, $accountId: UUID, $scopes: [ScopeType!]!) { generateUserAccessToken(userId: $userId, accountId: $accountId, scopes: $scopes) { token } }",
>     "variables": { "userId": "YOUR_USER_ID", "accountId": "YOUR_ACCOUNT_ID", "scopes": ["LIST_OFFERS"] }
>   }'
> ```
>
> 從回應中複製 `data.generateUserAccessToken.token`。
>
> **2. 取得商家,每次 20 個。** 從 `offset` 0 開始。每次加 20 並重複,直到 `getMerchants` 為空清單 `[]`。
>
> ```bash theme={null}
> curl -X POST https://transactional-graph.fluzapp.com/api/v1/graphql \
>   -H "Authorization: Bearer ACCESS_TOKEN" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "query": "query ($paginate: OffsetInput, $offerTypes: OfferTypesInput) { getMerchants(paginate: $paginate, offerTypes: $offerTypes) { name slug logoUrl categories { name } offers { offerRates { maxUserRewardValue } } } }",
>     "variables": { "paginate": { "limit": 20, "offset": 0 }, "offerTypes": { "giftCardOffer": true, "cardLinkedOffer": false } }
>   }'
> ```
>
> 對每個商家,以最高的 `maxUserRewardValue` 作為其現金回饋率,並建立其連結:
>
> ```text theme={null}
> https://fluz.app/store/{slug}?referred_by={your referral code}
> ```

## 檢查結果

1. 開啟 `fluz-feed.json`,確認其中列有商家與回饋率。
2. 在私人瀏覽視窗中開啟其中一個 `link`。它應該會導向該商家在 fluz.app 上的頁面。
3. 確認連結結尾的代碼與您的推薦碼完全一致,包括大小寫。錯誤的代碼不會顯示任何錯誤,而且該次註冊不會記入您名下。

## 保持資料最新

* 每天執行一次腳本,例如透過 cron。Fluz 每天更新目錄兩次,而且回饋率經常變動。
* 存取權杖的有效期很短。腳本每次執行都會取得新的權杖,因此您不需要管理它。
* 回饋率變動時,商家的 slug 不會改變,因此您已發布的連結仍然有效。

## 如果無法運作

> [accordion] Error verifying API basic token
>
> API Key 未被接受。請檢查:
>
> * 您是在 **fluz.app/for-developers** 建立應用程式,而不是在沙盒中。
> * 您已完整貼上 API Key,與顯示的內容完全相同。請不要再次編碼。
> * 金鑰沒有被重新產生。重新產生的金鑰會立即取代舊金鑰。
> * User ID 與 Account ID 和金鑰來自同一個應用程式。

> [accordion] 資料饋送中有商家,但回饋率為 0
>
> 有些商家目前沒有回饋率。您可以在網站上略過它們,或顯示時不附回饋率。當回饋率恢復時,它們會重新出現在資料饋送中。

> [accordion] 透過我的連結註冊的使用者沒有記入我名下
>
> 只有在對方於開啟您連結的同一個瀏覽器中建立**新的** Fluz 帳戶時,才會計入推薦。請確認連結中的推薦碼與您在 Fluz 的推薦碼完全一致。請參閱[推薦點數如何運作](https://docs.fluz.app/formatting-referral-links.md#how-referral-credit-works)。

> [accordion] 腳本在中途停止
>
> 腳本遇到任何錯誤就會停止,並印出 Fluz 的回應。請再執行一次。如果相同的錯誤重複出現,請將該訊息傳送給您的 Fluz 聯絡人。

## 更多詳細資訊

* [推薦連結](https://docs.fluz.app/formatting-referral-links.md):所有連結格式,以及推薦點數如何運作
* [取得目錄](https://docs.fluz.app/get-gift-card-merchant-catalog.md):`getMerchants` 的所有欄位與篩選條件
* [API 憑證](https://docs.fluz.app/get-started/api-credentials.md):存取權杖的運作方式


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.