使用大量内存的简短 PHP - cURL + XML阅读器

发布于 2025-01-05 23:51:13 字数 5193 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2025-01-12 23:51:13

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

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

)

*/
?>

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:

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

)

*/
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文