You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP调用Zendesk API创建工单时,如何在元数据中添加flags:[3]标识

Solution for Adding flags:[3] to Zendesk Ticket Metadata via PHP API

Hey there, let's fix this issue you're having with the Zendesk API! The core problem here is that you need to place the flags array directly under the metadata object in your request body, not nested inside the custom subfield (which is only for your own custom metadata).

Step-by-Step Implementation

Here's a working PHP example using cURL that correctly sets the flags:[3] to mark the requester as unlogged:

<?php
// Configure your Zendesk credentials
$zendeskSubdomain = "your-subdomain";
$adminEmail = "your-admin-email@your-domain.com";
$apiToken = "your-api-token";

// Build the ticket payload with correct metadata structure
$ticketPayload = [
    "ticket" => [
        "subject" => "Request from Unlogged User",
        "comment" => [
            "body" => "This ticket was submitted by a user who wasn't logged in."
        ],
        "metadata" => [
            "flags" => [3], // This is where the flags belong!
            // Optional: Add your custom metadata here if needed
            "custom" => [
                "user_type" => "anonymous",
                "source" => "public_contact_form"
            ]
        ]
    ]
];

// Set up cURL request
$curl = curl_init("https://{$zendeskSubdomain}.zendesk.com/api/v2/tickets.json");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($ticketPayload));
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: Basic " . base64_encode("{$adminEmail}/token:{$apiToken}")
]);

// Execute and handle response
$response = curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($statusCode === 201) {
    echo "Ticket created successfully with anonymous requester flag!";
} else {
    echo "Failed to create ticket. Response: " . $response;
}

curl_close($curl);
?>

Key Notes to Remember

  • Correct Payload Structure: The flags array lives directly in the metadata object of the ticket. Putting it inside custom will only add it to your custom metadata, which isn't what you want.
  • Flag Value Context: The 3 flag corresponds to anonymous_requester in Zendesk's system, which is exactly what you need to mark an unlogged user ticket.
  • Permissions: Make sure your API token has the necessary permissions to create tickets and modify metadata (most admin-level tokens will have this by default).
  • JSON Encoding: Double-check that you're properly encoding the payload to JSON—missing this step will cause the API to reject your request.

If you're using the official Zendesk PHP client instead of raw cURL, the logic stays the same: structure the metadata array with flags at the top level, not inside custom.

内容的提问来源于stack exchange,提问作者Dastageer khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:52:49