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): encode empty app inputs as JSON objects (#44675)

Winoren4d26f85847e0
diff --git a/sdks/php-client/README.md b/sdks/php-client/README.md
index 94bfc972085..d318e89a4cf 100644
--- a/sdks/php-client/README.md
+++ b/sdks/php-client/README.md
@@ -94,11 +94,29 @@ $response = $difyClient->message_feedback($message_id, $rating, "user_id");
Replace 'your-api-key-here' with your actual Dify API key.
+## Empty app inputs
+
+Apps without custom input variables can be called with an empty PHP array:
+
+```php
+$response = $chatClient->create_chat_message([], "Hello", "user_id", "blocking");
+```
+
+The SDK sends the top-level `inputs` as `{}`, as required by the API, for chat,
+completion and workflow requests. Nonempty input maps and nested arrays retain
+their values and JSON types.
+
## 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.
+After `composer install`, run these commands from this directory:
+
+```sh
+php tests/base_url_test.php
+php tests/input_object_test.php
+```
+
+The regressions use Guzzle's mock transport and do not contact an API or model provider.
diff --git a/sdks/php-client/dify-client.php b/sdks/php-client/dify-client.php
index 8af30a5481d..cfbdfbb9ec7 100644
--- a/sdks/php-client/dify-client.php
+++ b/sdks/php-client/dify-client.php
@@ -96,7 +96,7 @@ class DifyClient {
class CompletionClient extends DifyClient {
public function create_completion_message($inputs, $response_mode, $user, $files = null) {
$data = [
- 'inputs' => $inputs,
+ 'inputs' => $inputs === [] ? new stdClass() : $inputs,
'response_mode' => $response_mode,
'user' => $user,
'files' => $files,
@@ -108,7 +108,7 @@ class CompletionClient extends DifyClient {
class ChatClient extends DifyClient {
public function create_chat_message($inputs, $query, $user, $response_mode = 'blocking', $conversation_id = null, $files = null) {
$data = [
- 'inputs' => $inputs,
+ 'inputs' => $inputs === [] ? new stdClass() : $inputs,
'query' => $query,
'user' => $user,
'response_mode' => $response_mode,
@@ -190,7 +190,7 @@ class ChatClient extends DifyClient {
class WorkflowClient extends DifyClient{
public function run($inputs, $response_mode, $user) {
$data = [
- 'inputs' => $inputs,
+ 'inputs' => $inputs === [] ? new stdClass() : $inputs,
'response_mode' => $response_mode,
'user' => $user,
];
diff --git a/sdks/php-client/tests/input_object_test.php b/sdks/php-client/tests/input_object_test.php
new file mode 100644
index 00000000000..6f983fa5b5c
--- /dev/null
+++ b/sdks/php-client/tests/input_object_test.php
@@ -0,0 +1,53 @@
+<?php
+
+require dirname(__DIR__) . '/vendor/autoload.php';
+
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Middleware;
+use GuzzleHttp\Psr7\Response;
+
+$clients = [
+ ['ChatClient', 'create_chat_message', 'chat-messages'],
+ ['CompletionClient', 'create_completion_message', 'completion-messages'],
+ ['WorkflowClient', 'run', 'workflows/run'],
+];
+$cases = [[], ['name' => 'Ada', 'items' => ['a', 'b'], 'empty_items' => []], new stdClass()];
+$count = 0;
+foreach ($clients as [$clientClass, $method, $endpoint]) {
+ foreach ($cases as $inputs) {
+ $history = [];
+ $stack = HandlerStack::create(new MockHandler([new Response(200, [], '{"ok":true}')]));
+ $stack->push(Middleware::history($history));
+ $client = new $clientClass('test-api-key');
+ $property = new ReflectionProperty(DifyClient::class, 'client');
+ if (PHP_VERSION_ID < 80100) {
+ $property->setAccessible(true);
+ }
+ $property->getValue($client)->getConfig('handler')->setHandler($stack);
+ if ($clientClass === 'ChatClient') {
+ $client->$method($inputs, 'Hello', 'test-user', 'blocking');
+ } else {
+ $client->$method($inputs, 'blocking', 'test-user');
+ }
+
+ $request = $history[0]['request'];
+ $payload = json_decode((string) $request->getBody());
+ if (!is_object($payload->inputs)) {
+ throw new RuntimeException($clientClass . ' must send an input object; got ' . json_encode($payload->inputs));
+ }
+ if ((array) $payload->inputs !== (array) $inputs) {
+ throw new RuntimeException('Input values or nested arrays changed');
+ }
+ if ($request->getMethod() !== 'POST'
+ || $request->getUri()->getPath() !== '/v1/' . $endpoint
+ || $request->getHeaderLine('Authorization') !== 'Bearer test-api-key'
+ || $payload->user !== 'test-user'
+ || $payload->response_mode !== 'blocking') {
+ throw new RuntimeException('Request route, credentials or mode changed');
+ }
+ ++$count;
+ }
+}
+
+echo "Input object regression: {$count} requests passed\n";