convert file_put_contents to curl

1.1k Views Asked by At

I have a function to create cache data xml file on my server. I've used file_get_contents and file_put_contents, but now my hosting provider is restricting usage of them. The single way to use same function is to convert it to CURL. Can anyone give some ideas?

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);

Thanks for helping!

1

There are 1 best solutions below

0
On

Create a simple curl function that will return the response from the request and use it in place of file_get_contents. This is not tested but it should give you the general idea perhaps.

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;
}