- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在开发一个自定义 WordPress 插件,用于将 YouTube 内容集成到游戏博客中,为了限制每日 API 配额的压力,我一直在尝试想出一种方法来存储过期的临时缓存对象24小时后。
我创建了以下类来管理数据:
public class YouTubeDataCache {
protected $dataObject;
protected $expirationDate;
__construct($dataObject) {
$this->dataObject = $dataObject;
$this->expirationDate = time() + 86400;
}
public function isExpired() {
return $this->expirationDate < time();
}
public function getDataObject() {
return $this->dataObject;
}
}
json_encode($dataCache)
在此类的实例上生成我可以存储在数据库中的实例的 JSON 表示。
json_decode()
当我尝试调用 isExpired()
时,从数据库中拉出的字符串返回了未定义方法的 fatal error 在解码的对象上。 json_decode()
后保留对对象方法的访问权限? ? 最佳答案
花了一些时间来追踪这个问题,但这是我想出的解决方案。
如何在缓存中存储所有必要的数据元素?
具体细节会因个别用例而略有不同,但要点如下:json_encode()
只会编码 公众 变量。
因为我的$dataObject
和 $expirationDate
被定义为 protected
, json_encode()
无法访问这些值,因此编码了空白对象。哎呀。
再深入一点,从 YouTube API 返回的对象也包含很多 protected
数据元素,所以即使我将类变量更改为 public
,我在尝试存储视频缩略图等内容时仍然会遇到类似的问题。
最终,我不得不创建自己的序列化函数。
较新版本的 PHP 可以使用 JsonSerializable接口(interface)并定义一个特定于类的实现,根据文档,“返回可以由 json_encode()
序列化的数据”
由于我无法准确预测该插件的 PHP 用户将运行哪个版本,我选择只创建一个 toJSON()
方法,并修改构造函数以从 $dataObject
中提取特定数据元素.
一、构造函数/类变量:
对于我的特定用例,我只关心 ID、片段、状态和缩略图信息。如果您需要其他数据元素,则需要深入研究响应对象的结构,以查看您的序列化函数需要手动包含的内容。
YouTubeDataCache {
protected $objectId;
protected $snippet;
protected $status;
protected $thumbnails = array();
protected $expirationDate;
__construct($dataObject = null) {
$this->expirationDate = time() + 86400;
if ($dataObject === null) {
return; // I'll explain later
}
$this->objectId = $dataObject->getId();
$this->snippet = $dataObject->getSnippet();
$this->status = $dataObject->getStatus();
$thumbs = $dataObject->getThumbnails();
if ($thumbs->getDefault()) {
$this->addThumbnail('default', $thumbs->getDefault());
}
if ($thumbs->getMedium()) {
$this->addThumbnail('medium', $thumbs->getMedium());
}
if ($thumbs->getHigh()) {
$this->addThumbnail('high', $thumbs->getHigh());
}
if ($thumbs->getMaxRes()) {
$this->addThumbnail('maxRes', $thumbs->getMaxRes());
}
}
public function setExpirationDate($expirationDate) {
$this->expirationDate = $expirationDate;
}
public function addThumbnail($name, $data) {
$this->thumbnails[$name] = $data;
}
public function getObjectId() {
return $this->objectId;
}
public function setObjectId($objectId) {
$this->objectId = $objectId;
}
public function getSnippet() {
return $this->snippet;
}
public function setSnippet($snippet) {
$this->snippet = $snippet;
}
public function getStatus() {
return $this->status;
}
public function setStatus($status) {
$this->status = $status;
}
}
protected
相关联。大批。由于我最终需要将其从该数组中拉出,因此在这里执行此操作可为接下来的操作提供更一致的访问模式。
public function toJSON() {
$array = array(
'objectId' => $this->objectId,
'snippet' => $this->snippet,
'status' => $this->status,
'thumbnails' => $this->thumbnails,
'expirationDate' => $this->expirationDate
);
return json_encode($array);
}
$cacheObject->toJson()
生成数据的准确编码 JSON 表示,然后我可以将其写入数据库,这将我们带到解决方案的第二部分。
json_decode
创建一个
stdClass
实例或关联数组(取决于您选择调用函数的方式)。在这两种情况下,它都没有最初用于创建 JSON 字符串的类中的任何方法,因此我最终还必须创建自定义反序列化方法。
static
这样我就不需要缓存对象的实例来使用它。
public static function fromJSON($json) {
$data = json_decode($json);
$cacheObject = new YouTubeDataCache();
/* This is why the constructor now accepts a null argument.
I won't always have access to an API response object from
Google, and in such cases, it's faster to just call setters
on this class rather than jumping through hoops to create the
Google class instance. */
$cacheObject->setExpirationDate($data->expirationDate);
$cacheObject->setObjectId($data->objectId);
$cacheObject->setSnippet($data->snippet);
$cacheObject->setStatus($data->status);
foreach($data->thumbnails as $name => $thumbnail) {
$cacheObject->addThumbnail($name, $thumbnail);
}
return $cacheObject;
}
YouTubeDataCache {
protected $objectId;
protected $snippet;
protected $status;
protected $thumbnails = array();
protected $expirationDate;
__construct($dataObject = null) {
$this->expirationDate = time() + 86400;
if ($dataObject === null) {
return;
}
$this->objectId = $dataObject->getId();
$this->snippet = $dataObject->getSnippet();
$this->status = $dataObject->getStatus();
$thumbs = $dataObject->getThumbnails();
if ($thumbs->getDefault()) {
$this->addThumbnail('default', $thumbs->getDefault());
}
if ($thumbs->getMedium()) {
$this->addThumbnail('medium', $thumbs->getMedium());
}
if ($thumbs->getHigh()) {
$this->addThumbnail('high', $thumbs->getHigh());
}
if ($thumbs->getMaxRes()) {
$this->addThumbnail('maxRes', $thumbs->getMaxRes());
}
}
public function setExpirationDate($expirationDate) {
$this->expirationDate = $expirationDate;
}
public function addThumbnail($name, $data) {
$this->thumbnails[$name] = $data;
}
public function getObjectId() {
return $this->objectId;
}
public function setObjectId($objectId) {
$this->objectId = $objectId;
}
public function getSnippet() {
return $this->snippet;
}
public function setSnippet($snippet) {
$this->snippet = $snippet;
}
public function getStatus() {
return $this->status;
}
public function setStatus($status) {
$this->status = $status;
}
public function toJSON() {
$array = array(
'objectId' => $this->objectId,
'snippet' => $this->snippet,
'status' => $this->status,
'thumbnails' => $this->thumbnails,
'expirationDate' => $this->expirationDate
);
return json_encode($array);
}
public static function fromJSON($json) {
$data = json_decode($json);
$cacheObject = new YouTubeDataCache();
$cacheObject->setExpirationDate($data->expirationDate);
$cacheObject->setObjectId($data->objectId);
$cacheObject->setSnippet($data->snippet);
$cacheObject->setStatus($data->status);
foreach($data->thumbnails as $name => $thumbnail) {
$cacheObject->addThumbnail($name, $thumbnail);
}
return $cacheObject;
}
}
关于php - 使用 json_encode 临时存储来自 YouTube 数据 API 的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096303/
我已经设置了 Azure API 管理服务,并在自定义域上配置了它。在 Azure 门户中 API 管理服务的配置部分下,我设置了以下内容: 因为这是一个客户端系统,我必须屏蔽细节,但以下是基础知识:
我是一名习惯 React Native 的新程序员。我最近开始学习 Fetch API 及其工作原理。我的问题是,我找不到人们使用 API key 在他们的获取语句中访问信息的示例(我很难清楚地表达有
这里有很多关于 API 是什么的东西,但是我找不到我需要的关于插件 API 和类库 API 之间的区别。反正我不明白。 在 Documenting APIs 一书中,我读到:插件 API 和类库 AP
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我正在尝试找出设计以下场景的最佳方法。 假设我已经有了一个 REST API 实现,它将从不同的供应商那里获取书籍并将它们返回给我自己的客户端。 每个供应商都提供单独的 API 来向其消费者提供图书。
请有人向我解释如何使用 api key 以及它有什么用处。 我对此进行了很多搜索,但得到了不同且相互矛盾的答案。有人说 API key 是保密的,它从不作为通信的一部分发送,而其他人则将它发送给客户端
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
谁能告诉我为什么 WSo2 API 管理器不进行身份验证?我已经设置了两个 WSo2 API Manager 1.8.0 实例并创建了一个 api。它作为原型(prototype) api 工作正常。
我在学习 DSL 的过程中遇到了 Fluent API。 我在流利的 API 上搜索了很多……我可以得出的基本结论是,流利的 API 使用方法链来使代码流利。 但我无法理解——在面向对象的语言中,我们
基本上,我感兴趣的是在多个区域设置 WSO2 API 管理器;例如亚洲、美国和欧洲。一些 API 将部署在每个区域的数据中心内,而其他 API 将仅部署在特定区域内。 理想情况下,我想要的是一个单一的
我正在构建自己的 API,供以下用户使用: 1) 安卓应用 2) 桌面应用 我的网址之一是:http://api.chatapp.info/order_api/files/getbeers.php我的
我需要向所有用户显示我的站点的分析,但使用 OAuth 它显示为登录用户配置的站点的分析。如何使用嵌入 API 实现仪表板但仅显示我的网站分析? 我能想到的最好的可能性是使用 API key 而不是客
我正在研究大公司如何管理其公共(public) API。我想到的是拥有成熟 API 的公司,例如 Google、Facebook、Twitter 和 Amazon。 这些公司向公众公开了许多不同的 A
在定义客户可访问的 API 时,以下是首选的行业惯例: a) 定义一组显式 API 方法,每个方法都有非常狭窄和特定的目的,例如: SetUserName SetUserAge Se
这在本地 deserver 和部署时都会发生。我成功地能够通过留言簿教程使用 API 资源管理器,但现在我已经创建了自己的项目并尝试访问我编写的第一个 API,它从未出现过。搜索栏旁边的黄色“正在加载
我正在尝试使用 http://ip-api.com/ api通过我的ip地址获取经度和纬度。当我访问 http://ip-api.com/json从我的浏览器或使用 curl,它以 json 格式返回
这里的典型示例是 Twitter 的 API。我从概念上理解 REST API 的工作原理,本质上它只是针对您的特定请求向他们的服务器查询,然后您会在其中收到响应(JSON、XML 等),很棒。 但是
我能想到的最好的标题,但要澄清的是,情况是这样的: 我正在开发一种类似短 url 的服务,该服务允许用户使用他们的 Twitter 帐户“登录”并发布内容。现在这项服务可以包含在 Tweetdeck
我正在设计用于管理评论和讨论线程的 API 方案。我想有一个点 /discussions/:discussionId 当您GET 时,它会返回一组评论和一些元数据。评论也许可以单独访问 /discus
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭去年。 Improve this quest
我是一名优秀的程序员,十分优秀!