用asp與php實(shí)現(xiàn)百度ping服務(wù)的代碼
來源:易賢網(wǎng) 閱讀:1225 次 日期:2014-08-11 17:02:07
溫馨提示:易賢網(wǎng)小編為您整理了“用asp與php實(shí)現(xiàn)百度ping服務(wù)的代碼”,方便廣大網(wǎng)友查閱!

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>開發(fā)網(wǎng)站的一些小技巧</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>發(fā)送Ping到:" & Url & "</p>"

Response.Flush

Dim objPing

Set objPing = Server.CreateObject("MSXML2.ServerXMLHTTP")

objPing.SetTimeOuts 10000, 10000, 10000, 10000

‘第一個(gè)數(shù)值:解析DNS名字的超時(shí)時(shí)間10秒

‘第二個(gè)數(shù)值:建立Winsock連接的超時(shí)時(shí)間10秒

‘第三個(gè)數(shù)值:發(fā)送數(shù)據(jù)的超時(shí)時(shí)間10秒

‘第四個(gè)數(shù)值:接收response的超時(shí)時(shí)間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

‘===============================================

‘函數(shù)名:BytesToBstr

‘作用:字符集轉(zhuǎn)換

‘參數(shù):body–內(nèi)容;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失敗";

}

?>

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:用asp與php實(shí)現(xiàn)百度ping服務(wù)的代碼
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)