我有一个在我的服务器上创建缓存数据xml文件的功能。 我已经使用了file_get_contents和file_put_contents,但现在我的托管服务提供商限制了它们的使用。使用相同功能的单一方法是将其转换为CURL。任何人都可以提出一些想法吗?
function checkXmlCache($xmlQuery) {
$fibulabasexml="http://any url that give xml data/"; $cachedir="/tmp/"; $ageInSeconds = 36000;
$xmlQuery=str_replace(array("'",'"'),"",$xmlQuery);
$xmlQuery2=$xmlQuery;
$long=array("stars=","TURKEY","bestprice=","country=", "location=","hotelcode=","prices=yes","tara=","simple=yes","rand()","sort=","limit=","price ","hotelname"," desc","desc");
$short=array("ST-","TR","B-","C-", "L-", "H-", "PY-", "T-", "-S-", "-R-", "S-", "LT-", "PP-","HN","-D","-D");
$xmlQuery2=str_replace($long,$short,$xmlQuery2);
$xmlQuery2=str_replace(array("xmlhotels.php","xmllocations.php"),array("XH-","XL-"),$xmlQuery2);
$xmlQuery2=str_replace(array("&","?"),array(""),$xmlQuery2);
$xmlQuery2.="_.XML";
$xmlQuery2=strip_tags($xmlQuery2);
if(!file_exists($cachedir.$xmlQuery2) || (filemtime($cachedir.$xmlQuery2) + $ageInSeconds < (time() )) ) {
$contents = file_get_contents($fibulabasexml.str_replace(" ","%20",$xmlQuery));
if(strlen($contents)>200 ) { file_put_contents($cachedir.$xmlQuery2, $contents); }
}
return($cachedir.$xmlQuery2);
感谢您的帮助!
答案 0 :(得分:0)
创建一个简单的curl函数,该函数将返回请求中的响应并使用它代替file_get_contents
。这没有经过测试,但它应该给你一般的想法。
function checkXmlCache( $xml ) {
$url="http://any url that give xml data/";
$dir="/tmp/";
$age = 36000;
$xml=str_replace( array("'",'"'), "", $xml );
$query=$xml;
$long=array("stars=","TURKEY","bestprice=","country=", "location=","hotelcode=","prices=yes","tara=","simple=yes","rand()","sort=","limit=","price ","hotelname"," desc","desc");
$short=array("ST-","TR","B-","C-","L-","H-","PY-","T-","-S-","-R-","S-","LT-","PP-","HN","-D","-D");
$query=str_replace($long,$short,$query);
$query=str_replace(array("xmlhotels.php","xmllocations.php"),array("XH-","XL-"),$query);
$query=str_replace(array("&","?"),array(""),$query);
$query.="_.XML";
$query=strip_tags( $query );
if( !file_exists( $dir.$query ) || ( filemtime( $dir.$query ) + $age < ( time() ) ) ) {
$targeturl=$url . str_replace( " ", "%20", $xml );
/* call curl function rather than file_get_contents */
$contents = curl( $targeturl );
if( strlen( $contents ) > 200 ) { file_put_contents( $dir . $query, $contents ); }
}
return( $dir.$query );
}
function curl( $url ){
$ch=curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER,true );
curl_setopt( $ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT'] );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
$res=curl_exec( $ch );
curl_close( $ch );
return $res;
}