ZOHO PHPSDK uploadAttachment 接口无法上传文件的解决方法

表现原因:上传文件无响应

因为项目紧急,没空去找BUG,大致推测是因为curl上传导致的,这里直接新增一个上传方法,使用 GuzzleHttp 上传文件

  1. 在 ZCRMSDK/crm/api/APIRequest.php 文件中增加
/**
 * 使用 GuzzleHttp 上传文件
 *
 * @param UploadedFile $file 文件
 *
 * @return APIResponse
 *
 * @throws ZCRMException
 * @throws ZohoOAuthException
 */
public function uploadFileByGuzzleHttp(UploadedFile $file): APIResponse
{
    self::authenticateRequest();

    $client = new Client([
        'handler' => HandlerStack::create(),
        RequestOptions::HEADERS => array_merge($this->requestHeaders, [
            'User-Agent' => 'ZohoCRM PHP SDK',
            'Content-Type' => 'multipart/form-data',
        ]),
    ]);

    try {
        $response = $client->request($this->requestMethod, $this->url, [
            RequestOptions::MULTIPART => [
                [
                    'name' => 'file',
                    'contents' => fopen($file->getPathname(), 'r'),
                    'filename' => $file->getClientOriginalName(),
                ]
            ],
        ]);
    } catch (RequestException $e) {
        $response = $e->getResponse();
    } catch (GuzzleException $e) {
        throw new ZCRMException($e->getMessage());
    }

    $content = $response->getBody()->getContents();
    $headers = '';
    foreach ($response->getHeaders() as $key => $header) {
        $headers .= $key . ': ' . $header[0] . "\r\n";
    }

    $this->response = $headers . "\r\n\r\n" . $content;
    $this->responseInfo = [];

    return new APIResponse($this->response, $response->getStatusCode());
}
  1. 在 ZCRMSDK/crm/api/handler/RelatedListAPIHandler.php 文件中增加
class RelatedListAPIHandler extends APIHandler
{
	...其它方法
	
	/**
     * 通过 GuzzleHttp 上传文件
     *
     * @param UploadedFile $file
     *
     * @return APIResponse
     *
     * @throws ZCRMException
     * @throws ZohoOAuthException
     */
    public function uploadAttachmentByGuzzleHttp(UploadedFile $file): APIResponse
    {
        try {
            $this->requestMethod = APIConstants::REQUEST_METHOD_POST;
            $this->urlPath = $this->parentRecord->getModuleApiName() . "/" . $this->parentRecord->getEntityId() . "/" . $this->relatedList->getApiName();

            $apiRequest = APIRequest::getInstance($this);
            $responseInstance = $apiRequest->uploadFileByGuzzleHttp($file);

            return $this->callbackApiResponse($responseInstance);
        } catch (ZCRMException $exception) {
            APIExceptionHandler::logException($exception);
            throw $exception;
        }
    }
}
  1. 在 ZCRMSDK/crm/crud/ZCRMModuleRelation.php 文件中增加
class ZCRMModuleRelation
{
	...其它方法

	/**
     * 通过 GuzzleHttp 上传文件
     *
     * @param UploadedFile $file
     *
     * @return APIResponse
     *
     * @throws ZCRMException
     * @throws ZohoOAuthException
     */
    public function uploadAttachmentByGuzzleHttp(UploadedFile $file): APIResponse
    {
        return RelatedListAPIHandler::getInstance($this->parentRecord, $this)->uploadAttachmentByGuzzleHttp($file);
    }
}
  1. 在 ZCRMSDK/crm/crud/ZCRMRecord.php 文件中增加
class ZCRMRecord
{
	...其它方法

	/**
     * 通过 GuzzleHttp 上传文件
     *
     * @param UploadedFile $file
     *
     * @return APIResponse
     *
     * @throws ZCRMException
     * @throws ZohoOAuthException
     */
    public function uploadAttachmentByGuzzleHttp(UploadedFile $file): APIResponse
    {
        return ZCRMModuleRelation::getInstance($this, "Attachments")->uploadAttachmentByGuzzleHttp($file);
    }
}
  1. 调用
$record = ZCRMRecord::getInstance('模块名称', '记录ID');

// UploadedFile
$file = $request->file('file');

$response = $record->uploadAttachmentByGuzzleHttp($file);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值