2012-08-30 22 views
3

我很新使用cURL。有没有简单的方法来缓存cURL JSON请求的响应?这是我的要求...如何在PHP中缓存JSON cURL响应?

<?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); 
} 

?> 

谢谢!

+0

[从服务器高速缓存CURL JSON数据]的可能重复(http://stackoverflow.com/questions/6977563/caching-curl-json-data-from-server) –

回答

4

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

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

+0

虽然这种链路可以回答这个问题,最好在这里包含答案的重要部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/18800302) –