Php 在函数中使用curl而不是file\u get\u内容
下面是我加载和存储xml提要的代码。重要的是,如果提要脱机或响应速度慢,则超时也很重要 某些用户未启用文件\u获取\u内容。我正在寻找一种方法将其更改为curl,或者进行检查并使用启用的方法。并且不会丢失设置超时的功能。有什么想法吗Php 在函数中使用curl而不是file\u get\u内容,php,wordpress,file,file-get-contents,Php,Wordpress,File,File Get Contents,下面是我加载和存储xml提要的代码。重要的是,如果提要脱机或响应速度慢,则超时也很重要 某些用户未启用文件\u获取\u内容。我正在寻找一种方法将其更改为curl,或者进行检查并使用启用的方法。并且不会丢失设置超时的功能。有什么想法吗 function feeder() { $cache_time = 3600 * 12; // 12 hours $cache_file = plugin_dir_path(__FILE__) . '/cache/feed.xml';
function feeder()
{
$cache_time = 3600 * 12; // 12 hours
$cache_file = plugin_dir_path(__FILE__) . '/cache/feed.xml';
$timedif = @(time() - filemtime($cache_file));
$fc_xml_options = get_option('fc_xml_options');
$xml_feed = $fc_xml_options['feed'];
// remove white space(s) and/or space(s) from connector code
$xml_feed = str_replace(' ', '', $xml_feed);
$xml_feed = preg_replace('/\s+/', '', $xml_feed);
if (file_exists($cache_file) && $timedif < $cache_time)
{
$string = file_get_contents($cache_file);
}
else
{
// set a time-out (5 sec) on fetch feed
$xml_context = array( 'http' => array(
'timeout' => 5,
) );
$pure_context = stream_context_create($xml_context);
$string = file_get_contents($xml_feed, false, $pure_context);
if ($f = @fopen($cache_file, 'w'))
{
fwrite($f, $string, strlen($string));
fclose($f);
}
}
功能馈线()
{
$cache_time=3600*12;//12小时
$cache_file=plugin_dir_路径(_file)。'/cache/feed.xml';
$timedif=@(time()-filemtime($cache_file));
$fc_xml_options=get_option('fc_xml_options');
$xml_feed=$fc_xml_选项['feed'];
//删除连接器代码中的空白和/或空格
$xml\u feed=str\u replace('',$xml\u feed);
$xml\u feed=preg\u replace('/\s+/','','$xml\u feed);
如果(文件存在($cache\u文件)&&$timedif<$cache\u时间)
{
$string=file\u get\u contents($cache\u file);
}
其他的
{
//在获取提要上设置超时(5秒)
$xml_context=array('http'=>array(
“超时”=>5,
) );
$pure\u context=stream\u context\u create($xml\u context);
$string=file\u get\u contents($xml\u提要,false,$pure\u上下文);
如果($f=@fopen($cache_文件,'w'))
{
fwrite($f,$string,strlen($string));
外国法郎(f美元);
}
}
您可以使用curl读取本地文件。只需将url更改为前缀为file://
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "file://full_path_to_file");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
curl_close($ch);
开始翻译?我们修复代码,我们不翻译/重写它…如果FYGGCH()不可用,你可能想考虑卷也不会。