基于CURL的ping服务

ping服务,这里不再多讲,有很多的PING服务是基于XMLRPC的,今天,我共享的这个是基于CURL的,原理都是一样,都是把XML数据通过POST方法提交给PING服务器,下面的例子是把XML数据提交给百度的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
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">function</span> postUrl<span style="color: #009900">(</span><span style="color: #000088">$url</span><span style="color: #339933">,</span> <span style="color: #000088">$postvar</span><span style="color: #009900">)</span>
<span style="color: #009900">{</span>
    <span style="color: #000088">$ch</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: #000088">$headers</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">"POST "</span><span style="color: #339933">.</span><span style="color: #000088">$url</span><span style="color: #339933">.</span><span style="color: #0000ff">" HTTP/1.0"</span><span style="color: #339933">,</span>
        <span style="color: #0000ff">"Content-type: text/xml; charset=<span style="color: #000099;font-weight: bold">\"</span>gb2312<span style="color: #000099;font-weight: bold">\"</span>"</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">$postvar</span><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/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">$ch</span><span style="color: #339933">,</span> CURLOPT_URL<span style="color: #339933">,</span> <span style="color: #000088">$url</span><span style="color: #009900">)</span><span style="color: #339933">;</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">$ch</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>
    <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">$ch</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>
    <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">$ch</span><span style="color: #339933">,</span> CURLOPT_HTTPHEADER<span style="color: #339933">,</span> <span style="color: #000088">$headers</span><span style="color: #009900">)</span><span style="color: #339933">;</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">$ch</span><span style="color: #339933">,</span> CURLOPT_POSTFIELDS<span style="color: #339933">,</span> <span style="color: #000088">$postvar</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
    <span style="color: #000088">$res</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_exec" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_exec</span></a> <span style="color: #009900">(</span><span style="color: #000088">$ch</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
    <a rel="noopener noreferrer nofollow" href="http://www.php.net/curl_close" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">curl_close</span></a> <span style="color: #009900">(</span><span style="color: #000088">$ch</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
    <span style="color: #b1b100">return</span> <span style="color: #000088">$res</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
 
 
<span style="color: #000088">$baiduXML</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>WEB开发笔记</string></value></param>
   <param><value><string>http://www.chhua.com.com</string></value></param>
   <param><value><string>http://www.chhua.com/web-note2156 </string></value></param>
   <param><value><string>http://www.chhua.com</string></value></param>
   </params>
   </methodCall>"</span><span style="color: #339933">;</span>
<span style="color: #000088">$res</span> <span style="color: #339933">=</span> postUrl<span style="color: #009900">(</span><span style="color: #0000ff">'http://ping.baidu.com/ping/RPC2'</span><span style="color: #339933">,</span> <span style="color: #000088">$baiduXML</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
<span style="color: #b1b100">if</span> <span style="color: #009900">(</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strpos" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strpos</span></a><span style="color: #009900">(</span><span style="color: #000088">$res</span><span style="color: #339933">,</span> <span style="color: #0000ff">"<int>0</int>"</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">"PING成功"</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
<span style="color: #b1b100">else</span>
<span style="color: #009900">{</span>
 <span style="color: #b1b100">echo</span> <span style="color: #0000ff">"PING失败"</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">?></span>