Location: PHPKode > scripts > PHP AGI > phpagi-2.20/api-docs/__examplesource/exsource__home_phpagi_devel_phpagi_examples_ping.php_a8afcfe0902072cc636965d2b445e3fc.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<!-- template designed by Marco Von Ballmoos -->
			<title>Ping an IP address</title>
			<link rel="stylesheet" href="../media/stylesheet.css" />
			<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'/>
		</head>
		<body>
						<h1>Ping an IP address</h1>
<div class="listing">
<div class="src-code"><ol><li><div class="src-line">#!/usr/local/bin/php&nbsp;-q</div></li>
<li><div class="src-line"><span class="src-php">&lt;?php</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<a href="http://www.php.net/set_time_limit">set_time_limit</a><span class="src-sym">(</span><span class="src-num">0</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;require<span class="src-sym">(</span><span class="src-str">'phpagi.php'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$agi&nbsp;</span>=&nbsp;<span class="src-key">new&nbsp;</span><span class="src-id"><a href="../phpAGI/AGI.html">AGI</a></span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodanswer">answer</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-comm">//&nbsp;Play&nbsp;the&nbsp;&quot;Enter&nbsp;the&nbsp;host&nbsp;you&nbsp;wish&nbsp;to&nbsp;ping,&nbsp;followed&nbsp;by&nbsp;the&nbsp;pound&nbsp;sign&quot;&nbsp;prompt</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-comm">//&nbsp;and&nbsp;then&nbsp;play&nbsp;the&nbsp;beep.&nbsp;&nbsp;&nbsp;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodstream_file">stream_file</a><span class="src-sym">(</span><span class="src-str">'ping'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$result&nbsp;</span>=&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodget_data">get_data</a><span class="src-sym">(</span><span class="src-str">'beep'</span><span class="src-sym">,&nbsp;</span><span class="src-num">3000</span><span class="src-sym">,&nbsp;</span><span class="src-num">20</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$ip&nbsp;</span>=&nbsp;<a href="http://www.php.net/str_replace">str_replace</a><span class="src-sym">(</span><span class="src-str">'*'</span><span class="src-sym">,&nbsp;</span><span class="src-str">'.'</span><span class="src-sym">,&nbsp;</span><span class="src-var">$result</span><span class="src-sym">[</span><span class="src-str">'result'</span><span class="src-sym">]</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-comm">/*&nbsp;Danger&nbsp;Will&nbsp;Robinson!&nbsp;&nbsp;This&nbsp;does&nbsp;NOT&nbsp;properly&nbsp;escape&nbsp;the&nbsp;ping&nbsp;command!</span></div></li>
<li><div class="src-line"><span class="src-comm">&nbsp;&nbsp;&nbsp;*&nbsp;Someone&nbsp;could&nbsp;subvert&nbsp;your&nbsp;system&nbsp;if&nbsp;you&nbsp;don't&nbsp;fix&nbsp;this!&nbsp;-&nbsp;NO&nbsp;WARRANTY&nbsp;:P&nbsp;*/</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$execstr&nbsp;</span>=&nbsp;<span class="src-str">&quot;</span><span class="src-str">/bin/ping&nbsp;-c&nbsp;5&nbsp;-q&nbsp;-w&nbsp;9&nbsp;<span class="src-var">$ip</span>|grep&nbsp;transmitted</span><span class="src-str">&quot;</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-comm">//&nbsp;be&nbsp;polite.</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodstream_file">stream_file</a><span class="src-sym">(</span><span class="src-str">'thanks'</span><span class="src-sym">,&nbsp;</span><span class="src-str">'#'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$p&nbsp;</span>=&nbsp;<a href="http://www.php.net/popen">popen</a><span class="src-sym">(</span><span class="src-var">$execstr</span><span class="src-sym">,&nbsp;</span><span class="src-str">'r'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-key">if</span><span class="src-sym">(</span><span class="src-var">$p&nbsp;</span>==&nbsp;<span class="src-id">FALSE</span><span class="src-sym">)</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodtext2wav">text2wav</a><span class="src-sym">(</span><span class="src-str">&quot;</span><span class="src-str">Failed&nbsp;to&nbsp;ping&nbsp;<span class="src-var">$ip</span></span><span class="src-str">&quot;</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodconlog">conlog</a><span class="src-sym">(</span><span class="src-str">&quot;</span><span class="src-str">Failed&nbsp;to&nbsp;ping&nbsp;<span class="src-var">$execstr</span></span><span class="src-str">&quot;</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-key">else</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$str&nbsp;</span>=&nbsp;<span class="src-str">''</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-key">while</span><span class="src-sym">(</span><span class="src-sym">!</span><a href="http://www.php.net/feof">feof</a><span class="src-sym">(</span><span class="src-var">$p</span><span class="src-sym">))</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$r&nbsp;</span>=&nbsp;<a href="http://www.php.net/fgets">fgets</a><span class="src-sym">(</span><span class="src-var">$p</span><span class="src-sym">,&nbsp;</span><span class="src-num">1024</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-key">if</span><span class="src-sym">(</span><span class="src-sym">!</span><span class="src-var">$r</span><span class="src-sym">)&nbsp;</span><span class="src-key">break</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$str&nbsp;</span>.=&nbsp;<span class="src-var">$r</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-comm">//&nbsp;a&nbsp;minor&nbsp;hack.</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$str&nbsp;</span>=&nbsp;<a href="http://www.php.net/str_replace">str_replace</a><span class="src-sym">(</span><span class="src-str">'ms'</span><span class="src-sym">,&nbsp;</span><span class="src-str">'milli-seconds'</span><span class="src-sym">,&nbsp;</span><span class="src-var">$str</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-comm">//&nbsp;have&nbsp;festival&nbsp;read&nbsp;back&nbsp;the&nbsp;ping&nbsp;results.</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodtext2wav">text2wav</a><span class="src-sym">(</span><span class="src-str">&quot;</span><span class="src-str"><span class="src-var">$ip</span>&nbsp;-&nbsp;<span class="src-var">$str</span></span><span class="src-str">&quot;</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$agi</span><span class="src-sym">-&gt;</span><a href="../phpAGI/AGI.html#methodhangup">hangup</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-php">?&gt;</span></div></li>
</ol></div>
</div>
	<p class="notes" id="credit">
		Documentation generated on Thu, 30 Sep 2010 02:22:03 -0700 by <a href="http://www.phpdoc.org" target="_blank">phpDocumentor 1.4.2</a>
	</p>
	</body>
</html>
Return current item: PHP AGI