如何在PHP中缓存JSON cURL响应?

时间:2022-09-02 11:12:10

I'm pretty new to using cURL. Is there an easy way to cache the response of a cURL JSON request? Here is my request...

我很擅长使用cURL。有没有一种简单的方法来缓存cURL JSON请求的响应?这是我的要求......

<?php

error_reporting(E_ALL);

$url = "[JSON api call url]";

$timeout = 40;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

$str = curl_exec($ch);

curl_close($ch);

$data = array();

if ($str !== false) {
    $data = json_decode($str);
}

?>

Thanks!

谢谢!

1 个解决方案

#1


4  

I actually found this article that answered my question. Thanks!

我实际上发现这篇文章回答了我的问题。谢谢!

http://davidwalsh.name/php-cache-function

http://davidwalsh.name/php-cache-function

#1


4  

I actually found this article that answered my question. Thanks!

我实际上发现这篇文章回答了我的问题。谢谢!

http://davidwalsh.name/php-cache-function

http://davidwalsh.name/php-cache-function