function retrieveURLContentBySocket($url,
$host="",
$port=80,
$timeout=30){
if($host == ""){
if(!($pos = strpos($url,'://'))){
return false;
}
$host = substr( $url,
$pos+3,
strpos($url,'/',$pos+3) - $pos - 3);
$uri = substr($url,strpos($url,'/',$pos+3));
}
else{
$uri = $url;
}
$request = "GET ".$uri." HTTP/1.0rn"
."Host: ".$host."rn"
."Accept: */*rn"
."User-Agent: ZealGetrn"
."rn";
$sHnd = @fsockopen ($host, $port, $errno, $errstr, $timeout);
if(!$sHnd){
return false;
}
@fputs ($sHnd, $request);
// Get source
$result = "";
while (!feof($sHnd)){
$result .= fgets($sHnd,4096);
}
fclose($sHnd);
$headerend = strpos($result,"rnrn");
if (is_bool($headerend))
{
return $result;
}
else{
return substr($result,$headerend+4);
}
function convertIp($ip) {
$return = '';
if(preg_match("/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/", $ip)) {
$iparray = explode('.', $ip);
if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 && $iparray[1] == 168) || ($iparray[0] == 172 && ($iparray[1] >= 16 && $iparray[1] <= 31))) {
$return = '- LAN';
} elseif ($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
$return = '- Invalid IP Address';
} else {
$ipfile = MOOPHP_ROOT.'/plugins/ipdata/wry.dat';
if(!@file_exists($ipfile)) {
$return = convertIpFull($ip, $ipfile);
}
}
}
return $return;
}
function convertIpFull($ip, $ipdatafile) {
if(!$fd = @fopen($ipdatafile)) {
return '- Invalid IP data file';
}
$ip = explode('.', $ip);
$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
if(!($DataBegin = fread($fd, 4)) || !($DataEnd = fread($fd, 4)) ) return;
@$ipbegin = implode('', unpack('L', $DataBegin));
if($ipbegin < 0) $ipbegin += pow(2, 32);
@$ipend = implode('', unpack('L', $DataEnd));
if($ipend < 0) $ipend += pow(2, 32);
$ipAllNum = ($ipend - $ipbegin) / 7 + 1;
$BeginNum = $ip2num = $ip1num = 0;
$ipAddr1 = $ipAddr2 = '';
$EndNum = $ipAllNum;
while($ip1num > $ipNum || $ip2num < $ipNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);
fseek($fd, $ipbegin + 7 * $Middle);
$ipData1 = fread($fd, 4);
if(strlen($ipData1) < 4) {
fclose($fd);
return '- System Error';
}
$ip1num = implode('', unpack('L', $ipData1));
if($ip1num < 0) $ip1num += pow(2, 32);
if($ip1num > $ipNum) {
$EndNum = $Middle;
continue;
}
$DataSeek = fread($fd, 3);
if(strlen($DataSeek) < 3) {
fclose($fd);
return '- System Error';
}
$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
fseek($fd, $DataSeek);
$ipData2 = fread($fd, 4);
if(strlen($ipData2) < 4) {
fclose($fd);
return '- System Error';
}
$ip2num = implode('', unpack('L', $ipData2));
if($ip2num < 0) $ip2num += pow(2, 32);
if($ip2num < $ipNum) {
if($Middle == $BeginNum) {
fclose($fd);
return '- Unknown';
}
$BeginNum = $Middle;
}
}
$ipFlag = fread($fd, 1);
if($ipFlag == chr(1)) {
$ipSeek = fread($fd, 3);
if(strlen($ipSeek) < 3) {
fclose($fd);
return '- System Error';
}
$ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
fseek($fd, $ipSeek);
$ipFlag = fread($fd, 1);
}
if($ipFlag == chr(2)) {
$AddrSeek = fread($fd, 3);
if(strlen($AddrSeek) < 3) {
fclose($fd);
return '- System Error';
}
$ipFlag = fread($fd, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return '- System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while(($char = fread($fd, 1)) != chr(0))
$ipAddr2 .= $char;
$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
fseek($fd, $AddrSeek);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
} else {
fseek($fd, -1, SEEK_CUR);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
$ipFlag = fread($fd, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return '- System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while(($char = fread($fd, 1)) != chr(0))
$ipAddr2 .= $char;
}
fclose($fd);
if(preg_match('/http/i', $ipAddr2)) {
$ipAddr2 = '';
}
$ipaddr = "$ipAddr1 $ipAddr2";
$ipaddr = preg_replace('/CZ88.NET/is', '', $ipaddr);
$ipaddr = preg_replace('/^s*/is', '', $ipaddr);
$ipaddr = preg_replace('/s*$/is', '', $ipaddr);
if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
$ipaddr = '- Unknown';
}
return '- '.$ipaddr;
}
我们主要是用到php 调用google在线翻译功能哦,post一个远程地址再用curl读取google翻译后的值就OK了。class Google_API_translator {
public $out = "";
function translate() {
$this->out = "";
$text = urlencode("computer");//要翻译的单词
$google_translator_url = "http://translate.google.com/translate_a/t?client=t&text=".$text."&sl=en&tl=zh_CN";
//拼凑google翻译的api url
$gphtml = $this->postPage(array("url" => $google_translator_url));
$this->out = $gphtml;
return $this->out;
}
function postPage($opts) {
$html = "";
if($opts["url"] != "") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $opts["url"]);
$html = curl_exec($ch);
if(curl_errno($ch)) $html = "";
curl_close ($ch);
}
return $html;
}
}
$g = new Google_API_translator();
$g->translate();
很完美,返回的结果中没有任何编码问题。从google返回的中文编码完全正确。
接下来,能不能反向翻译,从中文到英文哪?
class Google_API_translator {
public $out = "";
function translate() {
$this->out = "";
$text = urlencode("计算机");//要翻译的单词
$google_translator_url = "http://translate.google.com/translate_a/t?client=t&text=".$text."&sl=zh_CN&tl=en";
echo $google_translator_url;
$gphtml = $this->postPage(array("url" => $google_translator_url));
$this->out = $gphtml;
return $this->out;
}
function postPage($opts) {
$html = "";
if($opts["url"] != "") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $opts["url"]);
$html = curl_exec($ch);
if(curl_errno($ch)) $html = "";
curl_close ($ch);
}
return $html;
}
}
$g = new Google_API_translator();
$g->translate();
问题出现了,返回的是一个乱码。诡异的是,这次接受的是google发送过来的英文单词,怎么会有编码错误?
是php的curl无法发送unicode编码或者google在接受的过程中出现了问题吗? 复制PHP内容到剪贴板 PHP代码:echo $google_translator_url;
得到的url是 复制PHP内容到剪贴板 PHP代码:http://translate.google.com/translate_a/t?client=t&text=%E8%AE%A1%E7%AE%97%E6%9C%BA&sl=zh_CN&tl=en
直接把这个url输入浏览器的地址栏,没有任何问题(IE,Firefox均能通过)。
假设途中unicode的传送出现了解析方面的问题,在api的url上更改了一下,把"&sl=zh_CN&tl=en"更改为完全错误的参数 复制PHP内容到剪贴板 PHP代码:http://translate.google.com/translate_a/t?client=t&text=%E8%AE%A1%E7%AE%97%E6%9C%BA&sl=en&tl=en
奇怪了这次php页面倒是能够接受到google发回来的中文字符串,但是死活就是无法显示使用正确的编码参数google发送过来结果。
php 控制iis 404出错页面,本文档主要是讲一下用php管理web站点时间在设置404无法找到的页面时所用的控制方法哦。<?
# PHP控制站点程序
#
# 编写人:韩湘子
#
# 邮箱:hanxiangzi@gmail.com
#
# MSN:hanxiangzi@gmail.com
#
# QQ:220670
#
# 欢迎大家互相联系讨论
?>
<link href="image/css.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
background-color: #D4D0C8;
}
-->
</style>
<?php
$Site = New Com("IIS://Localhost/w3svc/".$ServerID);
iF($Submit){
$Site->HttpErrors = "404,*,Url,".$httperr;
$Site->SetInfo();
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><a href="3.php"><strong>返回主机列表</strong></a></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><a href="33.php?ServerID=<?php echo $ServerID;?>">返回主机管理</a></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<form action="?ServerID=<?php echo $ServerID;?>" method="POST">
设置404错误 <input type=input value="" size=80 name="httperr"><input type="Submit" name="Submit" value="提交">
<br>格式:/路径/文件.htm,例如:/404.htm
</form>
<?
# PHP控制站点程序
#
# 编写人:韩湘子
#
# 邮箱:hanxiangzi@gmail.com
#
# MSN:hanxiangzi@gmail.com
#
# QQ:220670
#
# 欢迎大家互相联系讨论
?>
<link href="image/css.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
background-color: #D4D0C8;
}
-->
</style>
<?php
$Site = New Com("IIS://Localhost/w3svc/".$ServerID);
iF($Submit){
$Site->DefaultDoc = $DefaultDocList;
$Site->SetInfo();
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><a href="3.php"><strong>返回主机列表</strong></a></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><a href="33.php?ServerID=<?php echo $ServerID;?>">返回主机管理</a></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<form action="?ServerID=<?php echo $ServerID;?>" method="POST">
请输入默认文档,以英文符号,隔开 <input type=input value="<?php echo $Site->DefaultDoc;?>" size=80 name="DefaultDocList"><input type="Submit" name="Submit" value="提交">
</form>