开发者社区> 问答> 正文

智能云相册PHP SDK如何使用?

智能云相册PHP SDK如何使用?

展开
收起
小天使爱美 2020-03-31 13:52:41 709 0
1 条回答
写回答
取消 提交回答
  • 下面代码演示如何使用智能云相册的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); ?>
    2020-03-31 13:53:01
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-直面PHP微服务架构挑战-高驰涛 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载