Download the PHP package happyphper/dify without Composer
On this page you can find all versions of the php package happyphper/dify. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dify
Dify PHP SDK
A PHP SDK for Dify API, with support for the Hyperf framework.
中文文档
Requirements
- PHP >= 8.1
- Composer
- ext-fileinfo
Latest Updates (v1.2.0)
- Added token auto-refresh functionality for seamless handling of authentication token expiration
- Optimized error handling mechanism in
ConsoleClient - Added
clearRefreshTokenmethod 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
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
Usage with Hyperf
Publish Configuration
This will create the following files:
config/autoload/dify.php- Dify configuration file
Configuration
Add the following to your .env file:
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 filesredis: Redis cache, stores cache data in Redis
You can specify the cache driver and related configuration in the configuration file:
Complete Configuration File Structure
Below is the complete structure of the configuration file with all available options:
Basic Usage
Usage without Hyperf
Workflow Orchestration Chat API
Note: The Workflow Orchestration Chat API is planned for future releases. The following documentation outlines the planned functionality. Stay tuned for updates!
Basic Usage
Conversation Management
Message Feedback
File Upload and Multimodal Support
Audio Processing
Application Information
For more examples, please refer to the examples/workflow_example.php file.
Console API (Requires Login)
Some operations need to be performed through the Dify Console API, which requires login to obtain a token. The SDK provides a dedicated ConsoleClient class to handle these operations.
Note: The Console API is disabled by default. You need to set
DIFY_CONSOLE_ENABLE=truein your.envfile orconsole.enable = truein your configuration file to enable it. If you try to use theConsoleClientwhen the Console API is disabled, aConsoleDisabledExceptionwill be thrown.
Basic Usage
Token Auto-Refresh
The ConsoleClient now supports automatic token refresh, which handles token expiration seamlessly:
-
When an access token expires (401 error), the SDK will:
- Automatically attempt to refresh the token using the refresh token
- Retry the original request if refresh is successful
- Re-login if the refresh token is expired or not available
- Clear tokens and throw an exception if login fails
- The token refresh process is intelligent:
- Only clears the refresh token upon refresh failure
- Preserves the access token if it's still valid
- Handles all token management internally
This allows your application to focus on business logic without worrying about token management.
API Reference
Knowledge Base Operations
Document Operations
Segment Operations
Error Handling
The SDK provides comprehensive error handling mechanisms:
Advanced Usage
Handling Large Amounts of Data
When dealing with large amounts of data, it's recommended to use pagination and appropriate error retry mechanisms:
Troubleshooting
Common Issues
-
Invalid API Key: Ensure your API key is correct and has the required permissions.
-
Network Connection Issues: Check your network connection and firewall settings.
- Request Format Errors: Ensure the request parameters are in the correct format.
Enable Debugging
If you encounter issues, you can implement logging logic to capture request and response information to help debug problems.
Testing
Running Tests
Test Coverage
To generate the test coverage report, run:
This will generate an HTML coverage report in the coverage directory. You can open coverage/index.html in your browser to view detailed coverage information.
Current test coverage:
- Line Coverage: 95%
- Method Coverage: 90%
- Class Coverage: 100%
Complete API Documentation
For complete documentation of the Dify API, please refer to the Dify API Documentation.
License
MIT
All versions of dify with dependencies
psr/log Version ^1.0|^2.0|^3.0
hyperf/guzzle Version ^3.1
ext-fileinfo Version *