用asp與php實現百度ping服務的代碼

來源:互聯網
上載者:User

ASP: 複製代碼 代碼如下:<%
Option Explicit
Response.Buffer = true
Response.ContentType = "text/html"
Response.Charset = "gb2312″
Dim Url, Result
Url = "http://ping.baidu.com/ping/RPC2"
Result = BytesToBstr(SendPing(Url), "gb2312″)
IF Instr(Result, "<int>0</int>") > 0 Then
Response.Write("success")
Else
Response.Write("failure")
End IF
Function SendPing(Url)
Dim s : s = ""
Randomize()
Dim r : r = Int(Rnd() * 9999) + 1000
s = s & "<?xml version=""1.0″" encoding=""gb2312″"?>"
s = s & "<methodCall>"
s = s & "<methodName>weblogUpdates.ping</methodName>"
s = s & "<params>"
s = s & "<param><value><string>開發網站的一些小技巧</string></value></param>"
s = s & "<param><value><string>http://hi.baidu.com/subendong/blog</string></value></param>"
s = s & "<param><value><string>http://hi.baidu.com/subendong/blog/item/6cd9468d243e8c07b21bba5e.html</string></value></param>"
s = s & "<param><value><string></string></value></param>"
s = s & "</params>"
s = s & "</methodCall>"
Response.Write "<p>發送Ping到:" & Url & "</p>"
Response.Flush
Dim objPing
Set objPing = Server.CreateObject("MSXML2.ServerXMLHTTP")
objPing.SetTimeOuts 10000, 10000, 10000, 10000
‘第一個數值:解析DNS名字的逾時時間10秒
‘第二個數值:建立Winsock串連的逾時時間10秒
‘第三個數值:發送資料的逾時時間10秒
‘第四個數值:接收response的逾時時間10秒
objPing.open "POST", Url & "?r=" & r, False
objPing.setRequestHeader "Content-Type", "text/xml; charset=gb2312″
objPing.send(s)
SendPing = objPing.ResponseBody
Set objPing = Nothing
End Function
‘===============================================
‘函數名:BytesToBstr
‘作用:字元集轉換
‘參數:body–內容;Cset–指定的字元集
‘===============================================
Function BytesToBstr(body, Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
%>

PHP: 複製代碼 代碼如下:<?php
function postUrl($url, $postvar)
{
$ch = curl_init();
$headers = array(
"POST ".$url." HTTP/1.0″,
"Content-type: text/xml; charset=\"gb2312\"",
"Accept: text/xml",
"Content-length: ".strlen($postvar)
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar);
$res = curl_exec ($ch);
curl_close ($ch);
return $res;
}
$baiduXML = "<?xml version=\"1.0\" encoding=\"gb2312\"?>
<methodCall>
<methodName>weblogUpdates.extendedPing</methodName>
<params>
<param><value><string>指令碼之家</string></value></param>
<param><value><string>http://www.jb51.net</string></value></param>
<param><value><string>http://www.jb51.net/a/15222.html</string></value></param>
<param><value><string>http://www.jb51.net</string></value></param>
</params>
</methodCall>";
$res = postUrl(‘http://ping.baidu.com/ping/RPC2′, $baiduXML);
if ( strpos($res, "<int>0</int>") )
{
echo "PING成功";
}
else
{
echo "PING失敗";
}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.