2

我有一个在我的服务器上创建缓存数据 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);

感谢您的帮助!

4

1 回答 1

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;
}
于 2016-02-11T08:09:01.967 回答