使用php获取API缓存的问题
我在获取正在使用的API的缓存时遇到了一些问题 当使用php获取API缓存的问题,php,api,caching,Php,Api,Caching,我在获取正在使用的API的缓存时遇到了一些问题 当$cache->$do_url只是一个没有ID和密钥数组的文件时,下面的代码工作得非常好,但我遇到的问题是我必须传递API ID和密钥,以便API吐出我需要的信息 namespace Gilbitron\Util; class SimpleCache { // Path to cache folder (with trailing /) public $cache_path = 'cache/'; // Length o
$cache->$do_url
只是一个没有ID和密钥数组的文件时,下面的代码工作得非常好,但我遇到的问题是我必须传递API ID和密钥,以便API吐出我需要的信息
namespace Gilbitron\Util;
class SimpleCache {
// Path to cache folder (with trailing /)
public $cache_path = 'cache/';
// Length of time to cache a file (in seconds)
public $cache_time = 86400;
// Cache file extension
public $cache_extension = '.cache';
// This is just a functionality wrapper function
public function get_data($label, $url)
{
if($data = $this->get_cache($label)){
return $data;
} else {
$data = $this->do_curl($url);
$this->set_cache($label, $data);
return $data;
}
}
public function set_cache($label, $data)
{
file_put_contents($this->cache_path . $this->safe_filename($label) . $this->cache_extension, $data);
}
public function get_cache($label)
{
if($this->is_cached($label)){
$filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension;
return file_get_contents($filename);
}
return false;
}
public function is_cached($label)
{
$filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension;
if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) return true;
return false;
}
//Helper function for retrieving data from url
public function do_curl($url)
{
if(function_exists("curl_init")){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$content = curl_exec($ch);
curl_close($ch);
return $content;
} else {
return file_get_contents($url);
}
}
//Helper function to validate filenames
private function safe_filename($filename)
{
return preg_replace('/[^0-9a-z\.\_\-]/i','', strtolower($filename));
}
}
$cache = new SimpleCache();
$cache->cache_path = 'cache/';
$cache->cache_time = 86400;
if($data = $cache->get_cache('label')){
$data = json_decode($data);
} else {
$data = $cache->do_curl( 'http://data.leafly.com/locations/denver-relief/reviews?skip=0&take=12',
array(
'headers' => array(
'app_id' => 'MYIDHERE',
'app_key' => 'MYKEYHERE'
)
)
);
$cache->set_cache('label', $data);
$data = json_decode($data);
}
print_r($data);
有什么想法吗
用答案编辑
对于任何通过谷歌寻找类似问题的人,我找到了答案
我更改了将API链接拉到以下位置的do\u curl
行:
$data = $cache->do_curl( 'http://data.leafly.com/locations/denver-relief/reviews?skip=0&take=12' );
并在do_curl
函数中添加了以下行,以拉入标题app_id
和app_key
所需信息:
curl_setopt($ch, CURLOPT_HTTPHEADER,array('app_id: MYAPPID','app_key: MYAPPKEY'));
您的
公共函数do\u curl($url)
只有一个参数。为了传递头,需要添加如下参数:public function do\u curl($url,$header)
然后,可以使用以下代码添加这些标题:
curl\u setopt($ch,CURLOPT\u HTTPHEADER,$header)代码>
您的方法调用应该如下所示:
$data = $cache->do_curl(
'http://data.leafly.com/locations/denver-relief/reviews?skip=0&take=12',
array('app_id: MYIDHERE', 'app_key: MYKEYHERE')
);
请参考一下
另外,请看一下。祝贺您!似乎在我键入解决方案之前几秒钟,您就找到了解决方案:)不过还是要看一看,因为它包含了一个建议,说明如何通过将头作为variable@T3H40谢谢是的,在我发布并找到它之后,我在做一些搜索。既然你给出了正确的答案,我就把你的答案标为正确答案。非常感谢!很高兴我(本可以)帮忙:)