trying to read live rss, but its returning same xm

2019-05-23 10:03发布

问题:

I am trying to read xml file from "http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml" I understand its caching issue so i have added no-cache but stil its returning same file :(

<?php 
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Pragma: no-cache"); Header('Pragma: no-cache');

    $url = "http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml";

    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($curl);
    curl_close ($curl);
    print $result;
    $fp = fopen('score.xml', 'w');
    if($fp)
        fwrite($fp,  $result);  
    else
    echo "Error !";

    $url = "score.xml";
    $xml = simplexml_load_file($url);

    var_dump($xml);
 ?>

回答1:

Try setting the following options to prevent caching :

curl_setopt($curl, CURLOPT_FORBID_REUSE, 1); 
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); 

Docs on curl_setopt here