3

我对使用 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);
}

?>

谢谢!

4

0 回答 0