首页 > 解决方案 > Freshdesk API 创建带有附件抛出验证错误的注释

问题描述

use GuzzleHttp\Client;

function insert_freshdesk_note($ticketId, $content, $attachments=null)
{
    if(is_null($content)) {
        return false;
    }

    $url = 'https://mydomain.freshdesk.com/api/v2/tickets/'.$ticketId.'/notes';
    $method = 'POST';
    $userName = config('freshdesk_api_key');
    $password = 'password';

    $data = (!empty($attachments)) ? [
        "attachments[]" => $attachments,
        "body" => $content,
        "private" => false,
    ] : [
        "body" => $content,
        "private" => false,
    ];

    $options = (!empty($attachments)) ? [
        'json' => $data,
        'auth' => [$userName, $password],
        'headers' => ['content-type' => 'multipart/form-data']
    ] : [
        'json' => $data,
        'auth' => [$userName, $password]
    ];

    $client = new Client();

    try {
        $response = $client->request($method, $url, $options);
        return json_decode($response->getBody(), true);
    } catch (Exception $e) {
        Log::error($e);
    }
}

上面的代码在没有附件的情况下工作正常,但是当附件出现在图片中时,它会引发以下错误:-

GuzzleHttp\Exception\ClientException: Client error: `POST https://mydomain.freshdesk.com/api/v2/tickets/13033/notes` resulted in a `400 Bad Request` response:
{"description":"Validation failed","errors":[{"field":"{\"attachments","message":"Unexpected/invalid field in request","

我正在根据文档工作,到目前为止我已经走到了死胡同。我尝试了其他排列和组合,但通过这些,我无法解决这个问题。

谁能帮帮我吗。这是freshdesk文档的链接 并且在 $attachments[] = '@/path/to/xyz.ext' 中,这个特别的东西正在发生。

函数调用将如下所示:-

insert_freshdesk_note($this->freshdesk_ticket_id, $noteText, $image->image_full_path);

标签: phplaravelguzzlefreshdesk

解决方案


在上面的问题中,我尝试使用 GuzzleHTTP 客户端、Laravel 框架和 PHP 语言来实现注释的添加。以下是它开始工作的源代码。

use GuzzleHttp\Client;

function insert_freshdesk_note($ticketId, $content, $attachments=null)
{   
    if(is_null($content)) {
        return false;
    }

    $url = 'https://mydomain.freshdesk.com/api/v2/tickets/'.$ticketId.'/notes';
    $method = 'POST';
    $userName = config('freshdesk_api_key');
    $password = 'password';

    $attachmentsFilePath = explode('/',$attachments);
    $fileName = end($attachmentsFilePath);

    $options = (!empty($attachments)) ? [
        'multipart' => [
            [
                'name'     => 'body',
                'contents' => $content,
            ],
            [
                'name'     => 'private',
                'contents' => "false",
            ],
            [
                'name'     => 'attachments[]',
                'contents' => fopen($attachments, 'r'),
                'filename' => $fileName,
            ],
        ],
        'auth' => [$userName, $password],
    ] : [
        'json' => [
            "body" => $content,
            "private" => false,
        ],
        'auth' => [$userName, $password]
    ];

    $client = new Client();

    try {
        $response = $client->request($method, $url, $options);
        return json_decode($response->getBody(), true);
    } catch (Exception $e) {
        Log::error($e);
    }
}

我们必须以多部分方式发送数据,以便 Freshdesk 后端了解数据存在,通过在标题中写入 multipart/form-type 将无济于事。如果您还查看带有和不带有以下附件的文档:-

无附件:-

curl -v -u user@yourcompany.com:test -H "Content-Type: application/json" -X POST -d '{ "body":"Hi tom, Still Angry", "private":false, "notify_emails":["tom@yourcompany.com"] }' 'https://domain.freshdesk.com/api/v2/tickets/3/notes'

附:-

curl -v -u user@yourcompany.com:test -F "attachments[]=@/path/to/attachment1.ext" -F "body=Hi tom, Still Angry" -F "notify_emails[]=tom@yourcompany.com" -X POST 'https://domain.freshdesk.com/api/v2/tickets/20/notes'

可以看出卷曲的差异。这是我忽略的缺失部分。


推荐阅读