PHPackages                             happyphper/dify - 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. happyphper/dify

ActiveLibrary

happyphper/dify
===============

Dify PHP SDK

v1.2.2(1y ago)560MITPHPPHP &gt;=8.1

Since Mar 7Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (7)Versions (6)Used By (0)

Dify PHP SDK
============

[](#dify-php-sdk)

A PHP SDK for Dify API, with support for the Hyperf framework.

[中文文档](README-CN.md)

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Composer
- ext-fileinfo

Latest Updates (v1.2.0)
-----------------------

[](#latest-updates-v120)

- Added token auto-refresh functionality for seamless handling of authentication token expiration
- Optimized error handling mechanism in `ConsoleClient`
- Added `clearRefreshToken` method to clear only the refresh token
- Fixed error handling in paragraph update and delete operations
- Added support for regenerating sub-paragraphs
- Improved error handling mechanism
- Enhanced test coverage

Token Auto-Refresh Feature
--------------------------

[](#token-auto-refresh-feature)

Dify PHP SDK now supports token auto-refresh functionality, allowing your application to seamlessly handle authentication token expiration:

- **Automatic Token Refresh**: When an access token expires, the SDK automatically attempts to obtain a new access token using the refresh token
- **Automatic Request Retry**: After successful token refresh, the original request is automatically retried without manual intervention
- **Automatic Re-login**: If the refresh token has also expired, the SDK attempts to re-login using the configured credentials
- **Intelligent Token Management**: Only clears the refresh token upon refresh failure, preserving valid access tokens

These features allow your application to focus on business logic without worrying about the complexity of token management.

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

[](#installation)

```
composer require happyphper/dify
```

Usage with Hyperf
-----------------

[](#usage-with-hyperf)

### Publish Configuration

[](#publish-configuration)

```
php bin/hyperf.php vendor:publish happyphper/dify
```

This will create the following files:

- `config/autoload/dify.php` - Dify configuration file

### Configuration

[](#configuration)

Add the following to your `.env` file:

```
# Dify API Configuration
DIFY_DATASET_KEY=your_api_key_here        # Your Dify API key for dataset operations
DIFY_BASE_URL=https://api.dify.ai/v1      # Base URL for Dify API
DIFY_DEBUG=false                          # Enable/disable debug mode

# Console API Configuration (for operations requiring login)
DIFY_CONSOLE_ENABLE=false                 # Enable/disable console API
DIFY_CONSOLE_EMAIL=admin@ai.com           # Your Dify account email
DIFY_CONSOLE_PASSWORD=!Qq123123           # Your Dify account password

# Workflow API Configuration (for future releases)
DIFY_WORKFLOW_API_KEY=your_workflow_key_here  # Your Dify API key for workflow operations

# Dify Text Splitter Configuration
DIFY_TEXT_SPLITTER_TYPE=chunk             # Text splitting method: 'chunk' or 'paragraph'
DIFY_TEXT_SPLITTER_CHUNK_SIZE=1000        # Maximum size of each text chunk
DIFY_TEXT_SPLITTER_CHUNK_OVERLAP=200      # Overlap between adjacent chunks

# Dify Indexing Technique
DIFY_INDEXING_TECHNIQUE=high_quality      # Indexing technique: 'high_quality' or 'economy'

# Dify Cache Configuration
DIFY_CACHE_DRIVER=file                    # Cache driver: 'file' or 'redis'
DIFY_CACHE_PREFIX=dify_                   # Prefix for cache keys
DIFY_CACHE_TTL=86400                      # Cache TTL in seconds (24 hours)
DIFY_CACHE_FILE_DIRECTORY=/path/to/cache  # Directory for file cache

# Redis Cache Configuration (when using redis driver)
DIFY_CACHE_REDIS_HOST=127.0.0.1           # Redis host
DIFY_CACHE_REDIS_PORT=6379                # Redis port
DIFY_CACHE_REDIS_PASSWORD=null            # Redis password (null for none)
DIFY_CACHE_REDIS_DATABASE=0               # Redis database index
DIFY_CACHE_REDIS_TIMEOUT=0.0              # Redis connection timeout

```

### Cache Configuration

[](#cache-configuration)

Dify PHP SDK supports multiple cache drivers for storing authentication tokens and other information. Currently, the following cache drivers are supported:

- `file`: File cache, stores cache data in files
- `redis`: Redis cache, stores cache data in Redis

You can specify the cache driver and related configuration in the configuration file:

### Complete Configuration File Structure

[](#complete-configuration-file-structure)

Below is the complete structure of the configuration file with all available options:

```
