使用大量内存的简短 PHP - cURL + XML阅读器
我有一个 php 脚本,它运行一个简单的 cURL 请求并解析它以获取一些数据。其中四个进程在我的网站上为每个 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>
I have a php script that runs a simple cURL request and parses through it for some data. Four of these run per web user on my site, and in ps I see these processes using 12M of RAM. I have basically stripped everything I could out of this script as far as loops and decision making go, so I figure the memory is leaking in cURL but I'm not really sure what to do.
Am I missing a key opt for CURL? Is there something I can do to make this use less memory or processor time?
<?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
?>
Here is the XML it parses:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要使用不同的方法并迭代对象,并且将输出存储为您可能想要检索特定值的纯文本。这可能有帮助:
You may want to use a different approach and iterate through the object, and insted of storing the output as a plain text you may want to retrieve specific values. this may help: