分享下php读取远程文件的三种方法。
方法1,file_get_contents
代码:
<?php
$url = http://www.xxx.com/;
$contents = file_get_contents($url);
//以下避免出现中文乱码
//$getcontent = iconv("gb2312″, "utf-8″,file_get_contents($url));
//echo $getcontent;
echo $contents;
?>
有关file_get_contents的用法,请参考:
php file_get_contents抓取页面信息的代码
php file_get_contents函数抓取页面信息的代码
php file_get_contents函数代理获取远程页面的代码
php file_get_contents函数的使用问题
方法2,curl函数方法
代码:
<?php $url = "http:///"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //在需要用户检测的网页中,增加下面两行 //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD); $contents = curl_exec($ch); curl_close($ch); echo $contents; ?>
有关php curl的用法,请参考文章:
php中开启curl扩展的方法详解
php curl应用实例分析
php curl用法的实例代码
php curl 学习总结
方法3,php文件操作函数法,fopen->fread->fclose打开、读取、关闭文件句柄。
代码:
<?php
$handle = fopen ("http:///", "rb");
$contents = "";
do {
$data = fread($handle, 8192);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?>
php 文件操作函数的相关内容,请参考:
php文件操作函数的实例详解
php文件操作方法深入详解
php文件操作常用函数汇总
php目录与文件操作的实例教程
总结:
1,使用file_get_contents和fopen必须空间开启allow_url_fopen。
方法:编辑php.ini,设置 allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。
2,使用curl 必须空间开启curl。
建议打开URL时使用file_get_contents()方法,可优化打开速度。
分享下单位时间内对字符串进行加密与解密的函数。
可以指定时间内,对字符串进行加密还原,超时则无效。
多用于单点登录的token加密传输,临时密码等。
下面是实现代码:
<?php
/**
* @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
* @param string $key 密钥
* @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
* @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
* @edit www.
* @example
*
* $a = authcode('abc', 'ENCODE', 'key');
* $b = authcode($a, 'DECODE', 'key'); // $b(abc)
*
* $a = authcode('abc', 'ENCODE', 'key', 3600);
* $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
*/
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600) {
$ckey_length = 4;
// 随机密钥长度 取值 0-32;
// 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
// 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
// 当此值为 0 时,则不产生随机密钥
$key = md5($key ? $key : EABAX::getAppInf('KEY'));
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) :
sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) ==
substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
?>1,创建php类库文件-guid.php
<?php
// guid.php
class System {
function currentTimeMillis() {
list($usec, $sec) = explode(" ",microtime());
return $sec.substr($usec, 2, 3);
}
}
class NetAddress {
var $name = 'localhost';
var $ip = '127.0.0.1';
function getHost($coumputer_name, $ip) { // static
$address = new NetAddress();
$address->name = $coumputer_name;
$address->ip = $ip;
return $address;
}
function toString() {
return strtolower($this->name.'/'.$this->ip);
}
}
class Random {
function nextLong() {
$tmp = rand(0,1)?'-':'';
return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
}
}
class Guid{
var $valueBeforeMD5;
var $valueAfterMD5;
function Guid($computer_name, $ip){
$this->getGuid($computer_name, $ip);
}
//by www.
function getGuid($coumputer_name, $ip){
$address = NetAddress::getHost($coumputer_name, $ip);
$this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();
$this->valueAfterMD5 = md5($this->valueBeforeMD5);
}
function newGuid() {
$Guid = new Guid();
return $Guid;
}
function toString() {
$raw = strtoupper($this->valueAfterMD5);
return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
}
}
?>
2,调用示例,生成唯一的Gid:
<?php
require_once('guid.php'); //调用类库文件
$computer_name = $_SERVER["SERVER_NAME"];
$ip = $_SERVER["SERVER_ADDR"];
$guid = new Guid($computer_name, $ip);
print $guid->toString();
//输出结果:3238D32E-807C-B1C4-01C4-FD1346D32110
?>