1 单引号和双引号
<?php
$var= 12345;
echo $var; // 将打印$var 变量的值12345
echo '$var'; // 打印字符串'$var'
echo "$var"; // 将打印$var 变量的值12345
?>
单引号里无论有什么,都原样打印
2 header函数
header("location:http://www.baidu.com");
header("refresh:3;url=http://www.baidu.com");
3 HTTP验证
<?php
$username = 'username';
$password = 'password';
if (!isset( $PHP_AUTH_USER)){
Header( "WWW-Authenticate: Basic realm=\"PHP登录验证\"" );
Header( "HTTP/1.0 401 Unauthorized" );
echo "操作被中止 \n";
exit;
} else {
if ( ( $PHP_AUTH_USER == $userame ) && ( $PHP_AUTH_PW == $password ) ){
print( "您已经登录成功<br/>" );
} else {
print( "登录失败<br/>" );
}
}
?>
4 CURL
CURL 是client url library functions的缩写,主要用来做网络用的,PHP5里,可以把extension=php_curl.dll打开
举例如下
<?php
//初始化CURL
$ch = curl_init() or die(curl_error());
//设置URL参数
curl_setopt($ch, CURLOPT_URL,"http://www.baidu.com/s?wd=php5");
//要求CURL返回数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//执行请求
$result = curl_exec($ch) or die(curl_error());
//取得返回的结果并显示
echo $result;
echo curl_error($ch);
//关闭CURL
curl_close($ch);
?>
下载远端文件
<?php
function curl_download($remote, $local) {
$cp = curl_init($remote);
$fp = fopen($local, "w");
curl_setopt($cp, CURLOPT_FILE, $fp);
curl_setopt($cp, CURLOPT_HEADER, 0);
curl_exec($cp);
curl_close($cp);
fclose($fp);
}
//从远端取得文件,保存在本地硬盘d:\software目录下
curl_download("http://www.skycn.com/downloads/file.zip","d:/software");
?>
4 使用snoopy类
snoopy类是sourceforge.net上的项木,可以很轻松实现抓取
<?php
include "Snoopy.class.php";
$snoopy = new Snoopy;
$snoopy->fetch("http://bbs.sina.com.cn/index.html");
$line= $snoopy->results;
Print_r($line);
?>
自动登陆
<?php
include_once "Snoopy.class.php";
$snoopy = new Snoopy;
//您要提交的URL地址
$submit_url = "http://www.example.org/login/logging.php?action=login";
//表单上一些文本框参数
$submit_vars["loginmode"] = "normal";
$submit_vars["styleid"] = "1";
$submit_vars["username"] = "administrator"; //你的用户名
$submit_vars["password"] = "12345"; //你的密码
$submit_vars["loginsubmit"] = "提 交";
$snoopy->submit($submit_url,$submit_vars);
print $snoopy->results;
?>
<?php
$var= 12345;
echo $var; // 将打印$var 变量的值12345
echo '$var'; // 打印字符串'$var'
echo "$var"; // 将打印$var 变量的值12345
?>
单引号里无论有什么,都原样打印
2 header函数
header("location:http://www.baidu.com");
header("refresh:3;url=http://www.baidu.com");
3 HTTP验证
<?php
$username = 'username';
$password = 'password';
if (!isset( $PHP_AUTH_USER)){
Header( "WWW-Authenticate: Basic realm=\"PHP登录验证\"" );
Header( "HTTP/1.0 401 Unauthorized" );
echo "操作被中止 \n";
exit;
} else {
if ( ( $PHP_AUTH_USER == $userame ) && ( $PHP_AUTH_PW == $password ) ){
print( "您已经登录成功<br/>" );
} else {
print( "登录失败<br/>" );
}
}
?>
4 CURL
CURL 是client url library functions的缩写,主要用来做网络用的,PHP5里,可以把extension=php_curl.dll打开
举例如下
<?php
//初始化CURL
$ch = curl_init() or die(curl_error());
//设置URL参数
curl_setopt($ch, CURLOPT_URL,"http://www.baidu.com/s?wd=php5");
//要求CURL返回数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//执行请求
$result = curl_exec($ch) or die(curl_error());
//取得返回的结果并显示
echo $result;
echo curl_error($ch);
//关闭CURL
curl_close($ch);
?>
下载远端文件
<?php
function curl_download($remote, $local) {
$cp = curl_init($remote);
$fp = fopen($local, "w");
curl_setopt($cp, CURLOPT_FILE, $fp);
curl_setopt($cp, CURLOPT_HEADER, 0);
curl_exec($cp);
curl_close($cp);
fclose($fp);
}
//从远端取得文件,保存在本地硬盘d:\software目录下
curl_download("http://www.skycn.com/downloads/file.zip","d:/software");
?>
4 使用snoopy类
snoopy类是sourceforge.net上的项木,可以很轻松实现抓取
<?php
include "Snoopy.class.php";
$snoopy = new Snoopy;
$snoopy->fetch("http://bbs.sina.com.cn/index.html");
$line= $snoopy->results;
Print_r($line);
?>
自动登陆
<?php
include_once "Snoopy.class.php";
$snoopy = new Snoopy;
//您要提交的URL地址
$submit_url = "http://www.example.org/login/logging.php?action=login";
//表单上一些文本框参数
$submit_vars["loginmode"] = "normal";
$submit_vars["styleid"] = "1";
$submit_vars["username"] = "administrator"; //你的用户名
$submit_vars["password"] = "12345"; //你的密码
$submit_vars["loginsubmit"] = "提 交";
$snoopy->submit($submit_url,$submit_vars);
print $snoopy->results;
?>