教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php实现百度ping服务的代码

php实现百度ping服务的代码

发布时间:2017-11-02   编辑:jiaochengji.com
分享下asp与php实现百度ping服务的二段代码,有研究百度ping服务的朋友参考下。

首先,ASP实现百度ping服务。
 

复制代码 代码示例:
<%
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
%>

2,PHP实现百度ping服务。
 

复制代码 代码示例:
<?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.jbxue.com</string></value></param>
<param><value><string>http://www.jbxue.com/a/15222.html</string></value></param>
<param><value><string>http://www.jbxue.com</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失败";
}
?>

您可能感兴趣的文章:
php实现百度ping服务的代码
百度ping方法示例代码 自动ping百度
ping检测告警函数的shell脚本
python如何检测IP通不通
php 百度快照、百度收录、百度热词的代码分享
一个ping检测告警函数
PHP5中使用Web服务访问J2EE应用程序(1)
php 获取百度收录和百度快照时间的代码
go-micro php consul实现简单的微服务
linux下自动ping命令检测IP地址的shell脚本(图文)

关键词: 百度ping   
[关闭]
~ ~