使用大量内存的短PHP - cURL + XMLreader

时间:2012-02-16 11:23:15

标签: php xml performance curl memory-leaks

我有一个运行简单cURL请求的php脚本,并通过它解析一些数据。其中四个在我的站点上为每个Web用户运行,而在ps中我使用12M RAM看到这些进程。就循环和决策而言,我基本上已经从这个脚本中删除了所有内容,所以我认为内存在cURL中泄漏,但我不确定该怎么做。

我错过了CURL的关键选择吗?我能做些什么来减少内存或处理器的使用时间?

<?php
//Caching
$cachefile = '../../playlistcache/curplayingBOSUN_cache.html';
$cachetime = 18;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
exit;
}
ob_start(); // Start the output buffer


// URL to your Shoutcast server, including port
// don't put in the http:// part!  do it like you see here
// also, reverse DNS lookup needs to be on, or this probably won't work
$server = "SERVERADDRESS";

// Admin password for your Shoutcast server
$password = "SERVERPASSWORD";


$mysession = curl_init();
curl_setopt($mysession, CURLOPT_URL, "http://$server/admin.cgi?sid=1&mode=viewxml");
curl_setopt($mysession, CURLOPT_HEADER, false);
curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($mysession, CURLOPT_POST, false);
curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($mysession, CURLOPT_USERPWD, "admin:$password");
curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($mysession, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$xml = curl_exec($mysession);
curl_close($mysession);

$songs = "\n";
$xmlreader = new XMLReader();
$xmlreader->xml($xml);

//$xmlreader->next('SHOUTCASTSERVER');
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();
$xmlreader->read();

$songs .= "\n";
$songs .= $xmlreader->readString();
echo $songs;


// Cache the output to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser


?>

以下是它解析的XML:

<SHOUTCASTSERVER>
<CURRENTLISTENERS>2</CURRENTLISTENERS>
<PEAKLISTENERS>29</PEAKLISTENERS>
<MAXLISTENERS>300</MAXLISTENERS>
<UNIQUELISTENERS>2</UNIQUELISTENERS>
<AVERAGETIME>16318</AVERAGETIME>
<SERVERGENRE>Misc</SERVERGENRE>
<SERVERURL></SERVERURL>
<SERVERTITLE></SERVERTITLE>
<SONGTITLE>The Velvet Underground - White Light/White Heat</SONGTITLE>
<NEXTTITLE/>
<STREAMHITS>5093</STREAMHITS>
<STREAMSTATUS>1</STREAMSTATUS>
<STREAMPATH>/urbos.mp3</STREAMPATH>
<BITRATE>96</BITRATE>
<CONTENT>audio/mpeg</CONTENT>
<VERSION>2.0.0.29 (posix(linux x86))</VERSION>
<LISTENERS>
<LISTENER>
<HOSTNAME></HOSTNAME>
<USERAGENT>unregularradio/1.0 CFNetwork/548.0.4 Darwin/11.0.0</USERAGENT>
<CONNECTTIME>114</CONNECTTIME>
<UID>0xb3a0a0cc</UID>
</LISTENER>
<LISTENER>
<HOSTNAME></HOSTNAME>
<USERAGENT>
iTunes/10.5.3 (Windows; Microsoft Windows Vista Business Edition Service Pack 2 (Build     6002)) AppleWebKit/534.52.7
</USERAGENT>
<CONNECTTIME>32522</CONNECTTIME>
<UID>0xb4104f0c</UID>
</LISTENER>
</LISTENERS>
<SONGHISTORY>
<SONG>
<PLAYEDAT>1329390441</PLAYEDAT>
<TITLE>The Velvet Underground - White Light/White Heat</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329390265</PLAYEDAT>
<TITLE>Kim Hiorthøy - Torture Happiness</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329390087</PLAYEDAT>
<TITLE>Wild Adriatic - The Writer</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329390058</PLAYEDAT>
<TITLE>UNregular Radio - Lift Fest www.liftfest.com</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329389770</PLAYEDAT>
<TITLE>Mister Vertigo - Spear Hill</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329389627</PLAYEDAT>
<TITLE>The Pinkerton Thugs - Together</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329389360</PLAYEDAT>
<TITLE>Jack Johnson - Go On</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329389154</PLAYEDAT>
<TITLE>
Yung Damon!_My Show ft. Gucci Mane prod. by Big Hurt (Dirty)
</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329388975</PLAYEDAT>
<TITLE>Gayle Skidmore - Bad For Me</TITLE>
</SONG>
<SONG>
<PLAYEDAT>1329388741</PLAYEDAT>
<TITLE>Dubb Zero - Track two</TITLE>
</SONG>
</SONGHISTORY>
</SHOUTCASTSERVER>

1 个答案:

答案 0 :(得分:1)

您可能希望使用不同的方法并遍历该对象,并将输出存储为您可能想要检索特定值的纯文本。这可能有所帮助:

<?php 
header('Content-Type:text/html; charset=UTF-8');//Required for chars like ø
//do you curl and save results or use local copy.
$xml=file_get_contents('songs.xml');

$xmlreader = new XMLReader();
$xmlreader->xml($xml);

$i=0;
$replace=array("\r\n", "\r", "\n", "\t");
while ($xmlreader->read()) {
    if ($xmlreader->name === 'SONG' && $xmlreader->nodeType == XMLReader::ELEMENT) {
        $song[$i]['date']=substr(str_replace($replace,'',$xmlreader->readString()),0,10);
        $song[$i]['title']=substr(str_replace($replace,'',$xmlreader->readString()),10);
    $i++;
    }
}

//do your saving here, you could also json_encode the array for easy access.
print_r($song);
/*
Array
(
    [0] => Array
        (
            [date] => 1329390441
            [title] => The Velvet Underground - White Light/White Heat
        )

    [1] => Array
        (
            [date] => 1329390265
            [title] => Kim Hiorthøy - Torture Happiness
        )

    [2] => Array
        (
            [date] => 1329390087
            [title] => Wild Adriatic - The Writer
        )

    [3] => Array
        (
            [date] => 1329390058
            [title] => UNregular Radio - Lift Fest www.liftfest.com
        )

    [4] => Array
        (
            [date] => 1329389770
            [title] => Mister Vertigo - Spear Hill
        )

    [5] => Array
        (
            [date] => 1329389627
            [title] => The Pinkerton Thugs - Together
        )

    [6] => Array
        (
            [date] => 1329389360
            [title] => Jack Johnson - Go On
        )

    [7] => Array
        (
            [date] => 1329389154
            [title] => Yung Damon!_My Show ft. Gucci Mane prod. by Big Hurt (Dirty)
        )

    [8] => Array
        (
            [date] => 1329388975
            [title] => Gayle Skidmore - Bad For Me
        )

    [9] => Array
        (
            [date] => 1329388741
            [title] => Dubb Zero - Track two
        )

)

*/
?>