表现原因:上传文件无响应
因为项目紧急,没空去找BUG,大致推测是因为curl上传导致的,这里直接新增一个上传方法,使用 GuzzleHttp 上传文件
- 在 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());
}
- 在 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;
}
}
}
- 在 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);
}
}
- 在 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);
}
}
- 调用
$record = ZCRMRecord::getInstance('模块名称', '记录ID');
// UploadedFile
$file = $request->file('file');
$response = $record->uploadAttachmentByGuzzleHttp($file);