PHP code example of tourze / wechat-work-media-bundle
1. Go to this page and download the library: Download tourze/wechat-work-media-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tourze / wechat-work-media-bundle example snippets
use WechatWorkMediaBundle\Service\MediaService;
use WechatWorkMediaBundle\Enum\MediaType;
use WechatWorkBundle\Entity\Agent;
// Inject MediaService through dependency injection
public function __construct(private MediaService $mediaService)
{
}
// Upload a media file and get media ID
$agent = $this->agentRepository->find($agentId);
$mediaId = $this->mediaService->uploadAndGetMediaId(
$agent,
'/path/to/your/file.jpg',
MediaType::IMAGE
);
// Download a media file
$filePath = $this->mediaService->downloadMedia($agent, $mediaId, 'jpg');
use WechatWorkMediaBundle\Entity\TempMedia;
use WechatWorkMediaBundle\Enum\MediaType;
// Create a temporary media entity
$tempMedia = new TempMedia();
$tempMedia->setMediaId($mediaId);
$tempMedia->setType(MediaType::IMAGE);
$tempMedia->setFileUrl('/uploads/image.jpg');
$tempMedia->setAgent($agent);
$tempMedia->setExpireTime(new \DateTimeImmutable('+3 days'));
$entityManager->persist($tempMedia);
$entityManager->flush();
// Transform a file to WeChat Work material via JSON-RPC
$result = $jsonRpcClient->call('TransformFileToWechatWorkMaterial', [
'corpId' => 'your_corp_id',
'agentId' => 'your_agent_id',
'fileUrl' => 'uploads/document.pdf',
'mediaType' => 'file'
]);
$mediaId = $result['media_id'];
use WechatWorkMediaBundle\Exception\MediaUploadFailedException;
use WechatWorkMediaBundle\Exception\FileNotFoundException;
try {
$mediaId = $mediaService->uploadAndGetMediaId($agent, $filePath, MediaType::IMAGE);
} catch (MediaUploadFailedException $e) {
$logger->error('Media upload failed: ' . $e->getMessage());
// Handle upload error appropriately
} catch (FileNotFoundException $e) {
$logger->error('File not found: ' . $e->getMessage());
// Handle file not found error
}