Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(php-sdk): preserve custom API base URL paths (#44569)

Winoren09c854962dc8
diff --git a/sdks/php-client/README.md b/sdks/php-client/README.md
index 86237e826a0..94bfc972085 100644
--- a/sdks/php-client/README.md
+++ b/sdks/php-client/README.md
@@ -36,6 +36,9 @@ $conversation_id = null; // Start a new conversation.
$difyClient = new DifyClient($apiKey);
+// Self-hosted endpoints keep their full path with or without a trailing slash.
+$selfHostedClient = new DifyClient($apiKey, 'https://dify.example.com/dify/v1');
+
// Create a completion client
$completionClient = new CompletionClient($apiKey);
$response = $completionClient->create_completion_message(array("query" => "Who are you?"), "blocking", "user_id");
@@ -94,3 +97,8 @@ Replace 'your-api-key-here' with your actual Dify API key.
## License
This SDK is released under the MIT License.
+
+## Tests
+
+After `composer install`, run `php tests/base_url_test.php` from this directory.
+The regression uses Guzzle's mock transport and does not contact an API or model provider.
diff --git a/sdks/php-client/dify-client.php b/sdks/php-client/dify-client.php
index 82f2b6ebff1..8af30a5481d 100644
--- a/sdks/php-client/dify-client.php
+++ b/sdks/php-client/dify-client.php
@@ -9,7 +9,8 @@ class DifyClient {
public function __construct($api_key, $base_url = null) {
$this->api_key = $api_key;
- $this->base_url = $base_url ?? 'https://api.dify.ai/v1/';
+ // Endpoints are relative to the API directory, including for custom base URLs.
+ $this->base_url = rtrim($base_url ?? 'https://api.dify.ai/v1/', '/') . '/';
$this->client = new Client([
'base_uri' => $this->base_url,
'headers' => [
diff --git a/sdks/php-client/tests/base_url_test.php b/sdks/php-client/tests/base_url_test.php
new file mode 100644
index 00000000000..3855a7dfa15
--- /dev/null
+++ b/sdks/php-client/tests/base_url_test.php
@@ -0,0 +1,60 @@
+<?php
+
+require dirname(__DIR__) . '/vendor/autoload.php';
+
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Middleware;
+use GuzzleHttp\Psr7\Response;
+
+$cases = [
+ [null, 'https://api.dify.ai/v1/'],
+ ['https://dify.example.com/v1', 'https://dify.example.com/v1/'],
+ ['https://dify.example.com/v1/', 'https://dify.example.com/v1/'],
+ ['https://dify.example.com/dify/v1', 'https://dify.example.com/dify/v1/'],
+ ['https://dify.example.com/dify/v1/', 'https://dify.example.com/dify/v1/'],
+ ['https://dify.example.com', 'https://dify.example.com/'],
+ ['https://dify.example.com/', 'https://dify.example.com/'],
+];
+
+$clients = [
+ ['DifyClient', 'get_application_parameters', ['test-user'], 'GET', 'parameters?user=test-user', null],
+ ['ChatClient', 'stop_message', ['task-1', 'test-user'], 'POST', 'chat-messages/task-1/stop', ['user' => 'test-user']],
+ ['CompletionClient', 'create_completion_message', [[], 'blocking', 'test-user'], 'POST', 'completion-messages',
+ ['inputs' => [], 'response_mode' => 'blocking', 'user' => 'test-user', 'files' => null]],
+ ['WorkflowClient', 'stop', ['task-1', 'test-user'], 'POST', 'workflows/tasks/task-1/stop', ['user' => 'test-user']],
+];
+
+$count = 0;
+foreach ($clients as [$clientClass, $method, $args, $httpMethod, $endpoint, $payload]) {
+ foreach ($cases as [$baseUrl, $expectedBase]) {
+ $expectedUrl = $expectedBase . $endpoint;
+ $history = [];
+ $stack = HandlerStack::create(new MockHandler([new Response(200, [], '{"ok":true}')]));
+ $stack->push(Middleware::history($history));
+ $client = new $clientClass('test-api-key', $baseUrl);
+ // Replace only Guzzle's transport; retain the real constructor and URI resolution.
+ $property = new ReflectionProperty(DifyClient::class, 'client');
+ if (PHP_VERSION_ID < 80100) {
+ $property->setAccessible(true);
+ }
+ $property->getValue($client)->getConfig('handler')->setHandler($stack);
+
+ $response = $client->$method(...$args);
+ $request = $history[0]['request'];
+ if ((string) $request->getUri() !== $expectedUrl) {
+ throw new RuntimeException($clientClass . ': expected ' . $expectedUrl . ', got ' . $request->getUri());
+ }
+ if ($request->getMethod() !== $httpMethod
+ || $request->getHeaderLine('Authorization') !== 'Bearer test-api-key'
+ || $response->getStatusCode() !== 200) {
+ throw new RuntimeException('Request or response contract changed');
+ }
+ if ($payload !== null && json_decode((string) $request->getBody(), true) !== $payload) {
+ throw new RuntimeException('Request payload changed');
+ }
+ ++$count;
+ }
+}
+
+echo "Base URL regression: {$count} requests passed\n";