ping服务很好玩,大家可以试下,我写的这个类是基于curl和XML-RPC的,也就是说,通过CURL扩展把符合XML-RPC的数据进行提交,并把数据反馈,在这个类中,可以把反馈的数据用日志的方式记入日志文件。

我也还写过一个基于CURL的PING服务使用方法http://www.chhua.com/web-note2156 ),不过那一个是单一线程的,现在的这一个是多线程的,与大家共享,希望大家订阅和支持我的博客(www.chhua.com )也不枉陈华的汗水。

下面是ping服务类的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> ping<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$blog</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$pingUrl</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$pingBody</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$blog</span><span style="color: #339933">,</span><span style="color: #000088">$pingUrl</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//博客资料,ping服务器数组</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/extension_loaded" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">extension_loaded</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"curl"</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #b1b100">echo</span> <span style="color: #0000ff">"<script>alert('服务器没有打开CURL扩展,不能执行PING服务。');location.go(-1);</script>"</span><span style="color: #339933">;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">blog</span><span style="color: #339933">=</span><span style="color: #000088">$blog</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//把博客资料载入</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingUrl</span><span style="color: #339933">=</span><span style="color: #000088">$pingUrl</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//ping服务器资料载入</span>
	<span style="color: #009900">}</span>
	<span style="color: #666666;font-style: italic">//转载请注明web开发笔记-www.chhua.com</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> pingSubmit<span style="color: #009900">(</span><span style="color: #000088">$url</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
 
		<span style="color: #000088">$pingCurl</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_multi_init" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_multi_init</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//创建一个多线程句柄</span>
 
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingBody</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">getPingBody</span><span style="color: #009900">(</span><span style="color: #000088">$url</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取PING的数据部分</span>
 
		<span style="color: #000088">$pingHeader</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span>
		<span style="color: #0000ff">'Content-Type:text/xml; charset=UTF-8'</span><span style="color: #339933">,</span>
		<span style="color: #0000ff">'Accept:text/xml'</span><span style="color: #339933">,</span>
		<span style="color: #0000ff">"Content-length:"</span><span style="color: #339933">.</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/strlen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strlen</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingBody</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//设置头部信息</span>
 
		<span style="color: #b1b100">for</span> <span style="color: #009900">(</span><span style="color: #000088">$i</span><span style="color: #339933">=</span><span style="color: #cc66cc">0</span><span style="color: #339933">;</span><span style="color: #000088">$i</span><span style="color: #339933"><</span>sizeof<span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingUrl</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #000088">$i</span><span style="color: #339933">++</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_init" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_init</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//设置一个单独的CRUL句柄</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_setopt" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_setopt</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">,</span>CURLOPT_URL<span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingUrl</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//设置地址</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_setopt" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_setopt</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">,</span>CURLOPT_RETURNTRANSFER<span style="color: #339933">,</span><span style="color: #cc66cc">1</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//设置返回内容是以字符串方式</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_setopt" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_setopt</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">,</span>CURLOPT_TIMEOUT<span style="color: #339933">,</span><span style="color: #cc66cc">10</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//允许CURL执行的最长秒数</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_setopt" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_setopt</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">,</span>CURLOPT_HTTPHEADER<span style="color: #339933">,</span><span style="color: #000088">$pingHeader</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//设置头部数组</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_setopt" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_setopt</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">,</span>CURLOPT_POST<span style="color: #339933">,</span><span style="color: #cc66cc">1</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//POST方法提交</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_setopt" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_setopt</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">,</span>CURLOPT_POSTFIELDS<span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingBody</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//POST方法发送文件设置</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_multi_add_handle" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_multi_add_handle</span></a><span style="color: #009900">(</span><span style="color: #000088">$pingCurl</span><span style="color: #339933">,</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//将单独CURL句柄加入多线程</span>
		<span style="color: #009900">}</span>
 
		<span style="color: #b1b100">do</span> <span style="color: #009900">{</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_multi_exec" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_multi_exec</span></a><span style="color: #009900">(</span><span style="color: #000088">$pingCurl</span><span style="color: #339933">,</span><span style="color: #000088">$ping_flag</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span><span style="color: #b1b100">while</span> <span style="color: #009900">(</span><span style="color: #000088">$ping_flag</span><span style="color: #339933">></span><span style="color: #cc66cc">0</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
		<span style="color: #666666;font-style: italic">//保存返回的信息//转载请注明web开发笔记-www.chhua.com</span>
	<span style="color: #000088">$ping_log</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/date" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">date</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"Y-m-d H:i:s"</span><span style="color: #009900">)</span> <span style="color: #339933">.</span> <span style="color: #0000ff">"<span style="color: #000099;font-weight: bold">\r</span><span style="color: #000099;font-weight: bold">\n</span>"</span><span style="color: #339933">;</span>
	<span style="color: #b1b100">for</span> <span style="color: #009900">(</span><span style="color: #000088">$ping_count</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">0</span><span style="color: #339933">;</span> <span style="color: #000088">$ping_count</span> <span style="color: #339933"><</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/sizeof" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">sizeof</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingUrl</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #000088">$ping_count</span><span style="color: #339933">++</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000088">$ping_results</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_multi_getcontent" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_multi_getcontent</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$ping_count</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #000088">$ping_parser</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/xml_parser_create" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">xml_parser_create</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/xml_parse_into_struct" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">xml_parse_into_struct</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_parser</span><span style="color: #339933">,</span> <span style="color: #000088">$ping_results</span><span style="color: #339933">,</span> <span style="color: #000088">$ping_vals</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/xml_parser_free" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">xml_parser_free</span></a><span style="color: #009900">(</span><span style="color: #000088">$ping_parser</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #000088">$ping_log</span> <span style="color: #339933">.=</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingUrl</span><span style="color: #009900">[</span><span style="color: #000088">$ping_count</span><span style="color: #009900">]</span> <span style="color: #339933">.</span> <span style="color: #0000ff">"<span style="color: #000099;font-weight: bold">\r</span><span style="color: #000099;font-weight: bold">\n</span>"</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">foreach</span> <span style="color: #009900">(</span><span style="color: #000088">$ping_vals</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$ping_value</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
			<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$ping_value</span><span style="color: #009900">[</span><span style="color: #0000ff">'type'</span><span style="color: #009900">]</span> <span style="color: #339933">==</span> <span style="color: #0000ff">'complete'</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$ping_log</span> <span style="color: #339933">.=</span> <span style="color: #000088">$ping_value</span><span style="color: #009900">[</span><span style="color: #0000ff">'tag'</span><span style="color: #009900">]</span> <span style="color: #339933">.</span> <span style="color: #0000ff">'='</span> <span style="color: #339933">.</span> <span style="color: #000088">$ping_value</span><span style="color: #009900">[</span><span style="color: #0000ff">'value'</span><span style="color: #009900">]</span> <span style="color: #339933">.</span> <span style="color: #0000ff">"<span style="color: #000099;font-weight: bold">\r</span><span style="color: #000099;font-weight: bold">\n</span>"</span><span style="color: #339933">;</span>
			<span style="color: #009900">}</span>
		<span style="color: #009900">}</span>
		<span style="color: #000088">$ping_log</span> <span style="color: #339933">.=</span> <span style="color: #0000ff">"<span style="color: #000099;font-weight: bold">\r</span><span style="color: #000099;font-weight: bold">\n</span>"</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
	<a rel="noopener noreferrer nofollow" href="http://www.php.net/file_put_contents" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">file_put_contents</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/dirname" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">dirname</span></a><span style="color: #009900">(</span><span style="color: #009900;font-weight: bold">__FILE__</span><span style="color: #009900">)</span> <span style="color: #339933">.</span> <span style="color: #0000ff">'/ping.log'</span><span style="color: #339933">,</span> <span style="color: #000088">$ping_log</span><span style="color: #339933">,</span> FILE_APPEND<span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #666666;font-style: italic">//释放资源//转载请注明web开发笔记-www.chhua.com</span>
	<span style="color: #b1b100">for</span> <span style="color: #009900">(</span><span style="color: #000088">$ping_count</span><span style="color: #339933">=</span><span style="color: #cc66cc">0</span><span style="color: #339933">;</span> <span style="color: #000088">$ping_count</span><span style="color: #339933"><</span>sizeof<span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pingUrl</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #000088">$ping_count</span><span style="color: #339933">++</span><span style="color: #009900">)</span> <span style="color: #009900">{</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_multi_remove_handle" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_multi_remove_handle</span></a><span style="color: #009900">(</span><span style="color: #000088">$pingCurl</span><span style="color: #339933">,</span> <span style="color: #000088">$ping_ch</span><span style="color: #009900">[</span><span style="color: #000088">$ping_count</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #009900">}</span>
	<a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_multi_close" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_multi_close</span></a><span style="color: #009900">(</span><span style="color: #000088">$pingCurl</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000000;font-weight: bold">function</span> getPingBody<span style="color: #009900">(</span><span style="color: #000088">$url</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//转载请注明web开发笔记-www.chhua.com</span>
		<span style="color: #000088">$ping_body</span><span style="color: #339933">=</span><span style="color: #0000ff">"<?xml version=<span style="color: #000099;font-weight: bold">\"</span>1.0<span style="color: #000099;font-weight: bold">\"</span> encoding=<span style="color: #000099;font-weight: bold">\"</span>utf-8<span style="color: #000099;font-weight: bold">\"</span>?>
	   <methodCall>
	   <methodName>weblogUpdates.extendedPing</methodName>
	   <params>
	   <param><value><string>{<span style="color: #006699;font-weight: bold">$this->blog</span>["</span>name<span style="color: #0000ff">"]}</string></value></param>
	   <param><value><string>{<span style="color: #006699;font-weight: bold">$this->blog</span>["</span>url<span style="color: #0000ff">"]}</string></value></param>
	   <param><value><string><span style="color: #006699;font-weight: bold">{$url}</span></string></value></param>
	   <param><value><string>{<span style="color: #006699;font-weight: bold">$this->blog</span>["</span>rss<span style="color: #0000ff">"]}</string></value></param>
	   </params>
	   </methodCall>"</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$ping_body</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">?></span>

这个类中都进行了详细的注释,如果有不懂的问题可以加入 PHP高级群进行询问,群号是:63417682。