之前曾介绍过一篇查看网页源代码的文章,有兴趣的朋友可以先热下身:php 查看页面源代码 。
以下是今天要介绍的二个例子。
1,获取网页Html源代码:
$lines = file('http://www./');
foreach ($lines as $line_num => $line) {
echo "Line <b>{$line_num}</b> : " . htmlspecialchars()($line) . "<br />\n";
}
?>
2,获取网页的标题:
$url = 'http://www.';
$lines_array = file($url);
echo $lines_array;
echo "<br/>";
$lines_string = implode('', $lines_array);
eregi("<title>(.*)</title>", $lines_string, $head);
echo "head:".$head;
echo "<br/>";
print_r($head);
echo "<br/>";
echo "title:".$head[1];
?>
写了几个php函数,用来取得指定文件的信息,主要学习下file_exists、is_file、is_dir、is_readable、is_writeable、filetype方法的用法。
例子:
<?php
/**
* 功能:获取文件信息
* 编辑:www.
*/
//设置默认时区
date_default_timezone_set('PRC');
function getFileInfo($filePath){
if(!file_exists($filePath)){
echo '指定的文件不存在!';
return;
}
if(is_file($filePath)){
echo $filePath.'是一个文件'.'<br>';
}
if(is_dir($filePath)){
echo $filePath.'是一个目录'.'<br>';
}
echo '文件的形态:'.getFileType($filePath).'<br>';
echo '文件的大小:'.getFileSize($filePath).'<br>';
if(is_readable($filePath)){
echo '文件可读'.'<br>';
}else{
echo '文件不可读'.'<br>';
}
if(is_writeable($filePath)){
echo '文件可写'.'<br>';
}else{
echo '文件不可写'.'<br>';
}
echo '文件建立的时间:'.date('Y年m月d日',filectime($filePath)).'<br>';
echo '文件最后修改的时间:'.date('Y年m月d日',filemtime($filePath)).'<br>';
echo '文件最后访问的时间:'.date('Y年m月d日',fileatime($filePath)).'<br>';
}
function getFileType($filePath){
switch(filetype($filePath)){
case 'file':
$type.='普通文件';
break;
case 'dir':
$type.='目录文件';
break;
case 'block':
$type.='块设备文件';
break;
case 'char':
$type.='字符设备文件';
break;
case 'fifo':
$type.='命名管道文件';
break;
case 'link':
$type.='符号链接';
break;
case 'unknown':
$type.='未知文件类型';
break;
default:
$type.='没有检测到文件类型';
}
return $type;
}
function getFileSize($filePath){
$bytes=filesize($filePath);
//1TB=1024GB 1GB=1024MB 1MB=1024KB 1KB=1024B
if($bytes > pow(2,40)){
$size = round($bytes/pow(1024,4),2);
$unit = 'TB';
}elseif($bytes > pow(2,30)){
$size = round($bytes/pow(1024,3),2);
$unit = 'GB';
}elseif($bytes > pow(2,20)){
$size = round($bytes/pow(1024,2),2);
$unit = 'MB';
}elseif($bytes > pow(2,10)){
$size = round($bytes/pow(1024,1),2);
$unit = 'KB';
}else{
$size = $bytes;
$unit = 'Byte';
}
return $size.' '.$unit;
}
$filePath = $_SERVER['DOCUMENT_ROOT'].'/test/editor.php';
getFileInfo($filePath);
?>
以下是调用上面的方法的测试结果,进行测试的文件为:F:/www/test/index.php。
输出结果:
文件的大小:654 Byte
文件可读
文件可写
文件建立的时间:2013年05月31日
文件最后修改的时间:2013年06月01日
文件最后访问的时间:2013年05月31日
php实现新浪、腾讯与淘宝登录的例子。
代码:
<?php
session_start();
class openlogin{
public $_URL = "";
public $config = array();
public function __construct(){
$this->openlogin();
}
function openlogin(){
}
/*获取登陆页面URL*/
public function login_url(){
if(empty($this->config)){
return false;
}
$config = $this->config;
$login_url = $config['login_url'];
$_SESSION['state'] = $state = md5(uniqid(rand(), TRUE));
$array = array(
"response_type"=>"code",
"state" => $state,
"client_id"=>$config['appkey'],
"redirect_uri"=>urlencode( $config['redirect_uri'] )
);
$this->set($array);
$url = $this->combineURL(/blog_article/$login_url , $this->_param/index.html);
if($url){
@header("Location:".$url);
}else{
return false;
}
}
/*获取access_token*/
public function get_access_token(){
if(empty($this->config)){
return false;
}
$config = $this->config;
if(! $config['code'] = $_REQUEST['code'] ){
return false;
}
$url = $config['authorization_url'];
$state = $_SESSION['state'];
$array = array(
"grant_type"=>"authorization_code",
"client_id" => $config['appkey'],
"client_secret"=>$config['appsecret'],
"code"=>$config['code'],
"redirect_uri"=>urlencode( $config['redirect_uri'] ),
"state"=>$state
);
$this->set($array);
return $this->post_contents($url);
}
/* set $this->_param 数组*/
public function set($array) {
if(empty($array)){
return false;
}
$this->_param = array();
foreach($array as $name=>$value){
$this->_param[$name] = $value;
}
}
/**
* post_contents
* 服务器通过post请求获得内容
* @param string $url 请求的url,拼接后的
* @return string 请求返回的内容
*/
public function post_contents($url){
if(empty($url)){
return false;
}
$param = $this->combineURL("" , $this->_param);
$ch = curl_init();
// 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
// 抓取URL并把它传递给浏览器
$reponse = curl_exec($ch);
curl_close($ch);
return $reponse;
}
/**
* get_contents
* 服务器通过get请求获得内容
* @param string $url 请求的url,拼接后的
* @return string 请求返回的内容
*/
public function get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
//-------请求为空
if(empty($response)){
return false;
}
return $response;
}
/**
* combineURL
* 拼接url
* @param string $baseURL 基于的url
* @param array $keysArr 参数列表数组
* @return string 返回拼接的url
*/
public function combineURL(/blog_article/$baseURL,$keysArr/index.html){
if( $baseURL=="" ){
$combined = "";
}else{
$combined = $baseURL."?";
}
$valueArr = array();
foreach($keysArr as $key => $val){
$valueArr[] = "$key=$val";
}
$keyStr = implode("&",$valueArr);
$combined .= ($keyStr);
return $combined;
}
}
//php实现QQ登录
class qq_openlogin extends openlogin{
private $openname = "qq";
public $config = array(
"appkey"=>"your appkey",
"appsecret"=>"your appsecret",
"redirect_uri"=>"XXXXX",
"login_url" => "https://graph.qq.com/oauth2.0/authorize",
"scope"=>"get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,
get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,get_tenpay_addr",
"authorization_url"=>"https://graph.qq.com/oauth2.0/token"
);
function __construct()
{
$this->qq_openlogin();
}
function qq_openlogin(){
parent::__construct();
}
function get_access_token(){
$response = parent::get_access_token();
/*检测错误是否发生*/
if(strpos($response, "callback") !== false){
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos -1);
$msg = json_decode($response);
if(isset($msg->error)){
return false;
}
}
$params = array();
parse_str($response, $params);
/*access_token == $params[access_token]*/
/*获取 openid */
$response = $this->get_contents("https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token']);
//--------检测错误是否发生
if(strpos($response, "callback") !== false){
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos -1);
}
$user = json_decode($response);
if(isset($user->error)){
return false;
}
/*
获取用户信息需要参数:openid(用户的ID,与QQ号码一一对应),access_token(可通过使用Authorization_Code获取Access_Token 或来获取access_token有3个月有效期),oauth_consumer_key(用户appid),format(返回格式)
*/
/*数据库保存*/
$open_param = array(
"openid"=>$user->openid,
"access_token"=>$params['access_token']
);
//
$open_param['oauth_consumer_key'] = $this->config['appkey'];
$open_param['format'] = "json";
/*拼接url*/
$get_user_url = $this->combineURL("https://graph.qq.com/user/get_user_info",$open_param);
//猎取用户信息
$userinfo = $this->get_contents($get_user_url);
$userinfo = json_decode($userinfo);
return $userinfo;
}
}
//php实现微博登录
class weibo_openlogin extends openlogin{
private $openname = "weibo";
public $config = array(
"appkey"=>"your appkey",
"appsecret"=>"your appsecret",
"login_url" => "https://api.weibo.com/oauth2/authorize",
"redirect_uri"=>"XXXXXXX",
"authorization_url"=>"https://api.weibo.com/oauth2/access_token"
);
function __construct()
{
$this->qq_openlogin();
}
function qq_openlogin(){
parent::__construct();
}
function get_access_token(){
$response = parent::get_access_token();
$userinfo = json_decode($response);
return $userinfo;
}
}
//php实现淘宝登录
class taobao_openlogin extends openlogin{
private $openname = "taobao";
public $config = array(
"appkey"=>"your appkey",
"appsecret"=>"your appsecret",
"redirect_uri"=>"XXXXX",
"authorization_url"=>"https://oauth.taobao.com/token",
"login_url"=>"https://oauth.taobao.com/authorize"
);
function __construct()
{
$this->qq_openlogin();
}
function qq_openlogin(){
parent::__construct();
}
function get_access_token(){
$response = parent::get_access_token();
$userinfo = json_decode($response);
return $userinfo;
}
}
if($_GET['openname']){
$openname = $_GET['openname']."_openlogin";
$openlogin = new $openname();
if(!isset($_REQUEST['code'])){
//请求url
$url = $openlogin->login_url();
if(!$url){
echo "0";
exit();
}
}else{
if(isset($_REQUEST["state"]) && ($_SESSION['state'] != $_REQUEST["state"] )){
echo "1";
exit();
}
$rs = $openlogin->get_access_token();
print_r( $rs );
}
}
?>
附,人人网登陆代码。
<?php
class renren_openlogin extends openlogin{
private $openname = "renren";
public $config = array(
"appid"=>"your appid",
"appkey"=>"your appkey",
"appsecret"=>"your secret key",
"redirect_uri"=>"XXXXXX",
"authorization_url"=>"https://graph.renren.com/oauth/token",
"login_url"=>"https://graph.renren.com/oauth/authorize"
);
function __construct()
{
$this->qq_openlogin();
}
function qq_openlogin(){
parent::__construct();
}
function get_access_token(){
$response = parent::get_access_token();
$userinfo = json_decode($response);
return $userinfo;
/*
access_token:获取的Access Token;
expires_in:Access Token的有效期,以秒为单位;
refresh_token:用于刷新Access Token 的 Refresh Token,长期有效,不会过期;
scope:Access Token最终的访问范围,既用户实际授予的权限列表(用户在授权页面时,有可能会取消掉某些请求的权限)。关于权限的具体信息请参考
*/
}
/*获取登陆页面URL*/
public function login_url(){
if(empty($this->config)){
return false;
}
$config = $this->config;
$login_url = $config['login_url'];
$array = array(
"response_type"=>"code",
"client_id"=>$config['appid'],
"redirect_uri"=>urlencode( $config['redirect_uri'] )
);
$this->set($array);
$url = $this->combineURL(/blog_article/$login_url , $this->_param/index.html);
if($url){
@header("Location:".$url);
}else{
return false;
}
}
}