下面代码演示如何使用智能云相册的PHP SDK完成照片上传、获取照片列表、获取照片缩略图地址和删除照片等基本功能。
示例代码和SDK的目录结构如下:
|____aliyun-oss-php-sdk-2.2.4/ |____aliyun-php-sdk-cloudphoto/ |____aliyun-php-sdk-core/ |____cloudphoto-phpdemo.php cloudphoto-phpdemo.php的内容如下,请参考代码中的注释填写相关配置。
setProtocol("https"); $createTransactionRequest->setStoreName($storeName); $createTransactionRequest->setLibraryId($libraryId); $createTransactionRequest->setMd5(md5_file($localFile)); $createTransactionRequest->setSize(filesize($localFile)); $createTransactionRequest->setExt("jpg"); $createTransactionResponse = $cloudPhotoClient->getAcsResponse($createTransactionRequest); print_r($createTransactionResponse); $sessionId = $createTransactionResponse->Transaction->Upload->SessionId; $ossAccessKeyId = $createTransactionResponse->Transaction->Upload->AccessKeyId; $ossAccessKeySecret = $createTransactionResponse->Transaction->Upload->AccessKeySecret; $ossStsToken = $createTransactionResponse->Transaction->Upload->StsToken; $bucket = $createTransactionResponse->Transaction->Upload->Bucket; $objectKey = $createTransactionResponse->Transaction->Upload->ObjectKey; $endPoint = $createTransactionResponse->Transaction->Upload->OssEndpoint; $fileId = $createTransactionResponse->Transaction->Upload->FileId; // 1.2 上传文件到OSS $ossClient = new \OSS\OssClient($ossAccessKeyId, $ossAccessKeySecret, $endPoint, false, $ossStsToken); $ossClient->uploadFile($bucket, $objectKey, $localFile); // 1.3 创建照片对象 $createPhotoRequest = new CloudPhoto\CreatePhotoRequest(); $createPhotoRequest->setProtocol("https"); $createPhotoRequest->setStoreName($storeName); $createPhotoRequest->setLibraryId($libraryId); $createPhotoRequest->setFileId($fileId); $createPhotoRequest->setSessionId($sessionId); $createPhotoResponse = $cloudPhotoClient->getAcsResponse($createPhotoRequest); print_r($createPhotoResponse); // photoId是个64bit的数字,可能很大,将其转为字符串。 $photoId = number_format($createPhotoResponse->Photo->Id, 0, '', ''); print("photo id = " . $photoId) . PHP_EOL; // 演示2:获取照片列表 $listPhotoRequest = new CloudPhoto\ListPhotosRequest(); $listPhotoRequest->setProtocol("https"); $listPhotoRequest->setStoreName($storeName); $listPhotoRequest->setLibraryId($libraryId); $listPhotoRequest->setCursor(0); $listPhotoRequest->setDirection("forward"); $listPhotoRequest->setSize(20); $listPhotoRequest->setState("active"); $listPhotoResponse = $cloudPhotoClient->getAcsResponse($listPhotoRequest); print_r($listPhotoResponse); // 演示3:获取照片缩略图地址 $getThumbnailRequest = new CloudPhoto\GetThumbnailRequest(); $getThumbnailRequest->setProtocol("https"); $getThumbnailRequest->setStoreName($storeName); $getThumbnailRequest->setLibraryId($libraryId); $getThumbnailRequest->setPhotoId($photoId); $getThumbnailRequest->setZoomType("image/resize,h_100"); // 缩放格式参考:https://help.aliyun.com/document_detail/44688.html $getThumbnailResponse = $cloudPhotoClient->getAcsResponse($getThumbnailRequest); print_r($getThumbnailResponse); // 演示4:删除照片 $deletePhotoRequest = new CloudPhoto\DeletePhotosRequest(); $deletePhotoRequest->setProtocol("https"); $deletePhotoRequest->setStoreName($storeName); $deletePhotoRequest->setLibraryId($libraryId); $deletePhotoRequest->setPhotoIds(array($photoId)); $deletePhotoResponse = $cloudPhotoClient->getAcsResponse($deletePhotoRequest); print_r($deletePhotoResponse); ?>版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。