WEB开发网
开发学院WEB开发PHP 面向PHP开发人员的XML 第3部分: 读、操纵和写XML的... 阅读

面向PHP开发人员的XML 第3部分: 读、操纵和写XML的高级技术

 2008-11-19 22:26:04 来源:WEB开发网   
核心提示: 清单 3 在 清单 2 所示 Simplexml(标准化越来越近了) 例子的基础上增加了缓存层,任何查询结果都能缓存两个小时,面向PHP开发人员的XML 第3部分: 读、操纵和写XML的高级技术(4),清单 3. 带有缓存层的 Yahoo Simplexml(标准化越来越近了) 例子<?php//This qu

清单 3 在 清单 2 所示 Simplexml(标准化越来越近了) 例子的基础上增加了缓存层。任何查询结果都能缓存两个小时。

清单 3. 带有缓存层的 Yahoo Simplexml(标准化越来越近了) 例子

<?php
//This query does a search for any Web pages relevant to "xml(标准化越来越近了) Query"
$query = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?".
"query=%5C%22xml(标准化越来越近了)%20Query%5C%22&appid=YahooDemo";
//The cached material should only last for 2 hours, so you need the
//current time.
$currentTime = microtime(true);
//This is where I put my tempfile; you can store yours in a more
//convenient location.
$cache = 'c:tempyws_'.md5($query);
//First check for an existing version of the time, and then check
//to see whether or not it's expired.
if(file_exists($cache) &&
filemtime($cache) > (time()-7200)) {
//If there's a valid cache file, load its data.
$data = file_get_contents($cache);
} else {
//If there's no valid cache file, grab a live version of the
//data and save it to a temporary file. Once the file is complete,
//copy it to a permanent file. (This prevents concurrency issues.)
$data = file_get_contents($query);
$tempName = tempnam('c:temp','YWS');
file_put_contents($tempName, $data);
rename($tempName, $cache);
}
//Wherever the data came from, load it into a Simplexml(标准化越来越近了) object.
$xml(标准化越来越近了) = simplexml(标准化越来越近了)_load_string($data);
//From here, the rest of the file is the same.
// Load up the root element attributes
foreach($xml(标准化越来越近了)->attributes() as $name=>$attr) {
$res[$name]=$attr;
}
...

上一页  1 2 3 4 5  下一页

Tags:面向 PHP 开发

编辑录入:coldstar [复制链接] [打 印]
赞助商链接