我有一个正在开发的应用程序的代码:
<?php
header("Content-type: text/xml");
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = str_replace( array('<abstract>', '</abstract>'), array('<description>', '</description>'), $data);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
echo $results->asXML();
?>
我有这一行: $thequery = urlencode($_GET['s']);
我想将其记录到数据库中,但输出 XML 显然非常重要那么我如何确保,如果 mysql 位由于某种原因失败,XML 仍然会输出?还是我把这个问题过于复杂化了?
最佳答案
使用ob_start()确保执行数据库操作时没有输出返回到浏览器/客户端
关于PHP 脚本在 mysql 失败时继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4244796/