It’s better to 1st save the returned XML to a file and then loop back to unset.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.cpac.ca/tip-podcast/jwplayer.xml",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 28025ee8-1e82-ce60-f6ae-f401118baa1c"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$fp = fopen(ABSPATH.'jwp.xml', 'w');
fwrite($fp, $response);
fclose($fp);
}
$xml = simplexml_load_file(ABSPATH.'jwp.xml');
for($i = count($xml->channel->item); $i >= 2; $i--){
unset($xml->channel->item[$i]);
}
$xml->saveXML(ABSPATH.'jwp.xml');
?>
<script src="https://content.jwplatform.com/libraries/FZ8yNTef.js"></script>
<center><div id="podcast" align="center"></div></center>
<script>
var PodcastplayerInstance = jwplayer("podcast");
PodcastplayerInstance.setup({
playlist: "<?php echo site_url(); ?>/jwp.xml",
androidhls: true,
preload: "auto",
height: 200,
width: 400,
visualplaylist:false,
stretching: "fill",
"plugins": {
"http://www.cpac.ca/tip-podcast/listy.js":{},
'viral-2': {'oncomplete':'False','onpause':'False','functions':'All'}
}
});
</script>
In case you want the 2nd or 3rd element only, update the above code with the following
for($i = count($xml->channel->item); $i >= 3; $i--){
unset($xml->channel->item[$i]);
}
for($i = 0; $i < count($xml->channel->item); $i++){
unset($xml->channel->item[0]);
}