❶ php怎么响应客户端发送http请求
http请求有get,post。
php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。
1. file_get_contents();详情见:http://www.jb51.net/article/41833.htm
2. curl发送请求。
3. fsocket发送。
下面说使用curl发送。
首先环境需要配置好curl组件。
在windows中让php支持curl比较简单:
在php.ini中将extension=php_curl.dll前面的分号去掉,
有人说需要将php根目录的libeay32.dll和ssleay32.dll需要拷贝到系统目录下去。我实验不拷贝也可以。
在linux中,如果使用源码安装,需要在make 之前,./configure --with-curl=path,
其中,path是你的 libcurl库的位置,比如你安装libcurl库之后,
path可能就是/usr/local/,libcurl可以是静态库,也可以是动态库。
注意libcurl库configure的时候,可以将一些不需要的功能去掉,
比如ssl , ldap等。在php configure的时候,会去检查libcurl中某些功能是否被开启,进而去相应地调整生成的php。
❷ php如何获取中文字符长度,一个中文字符算一个
在PHP中专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数并非PHP的核心函数,所以,它们常常有可能没有开启。要在php.ini中开启即可。获取长度实例:1 ,utf-8环境下使用
header('Content-type:text/html;charset=utf-8');
/**
*可以统计中文字符串长度的函数
*@param$str要计算长度的字符串
*@param$type计算长度类型,0(默认)表示一个中文算一个字符,1表示一个中文算两个字符
*
*/
functionabslength($str)
{
if(empty($str)){
return0;
}
if(function_exists('mb_strlen')){
returnmb_strlen($str,'utf-8');
}
else{
preg_match_all("/./u",$str,$ar);
returncount($ar[0]);
}
}
$str='我们都是中国人啊,ye!';
$len=abslength($str);
var_mp($len);//return12
$len=abslength($str,'1');
echo'<br/>'.$len;//return22
/*
utf-8编码下截取中文字符串,参数可以参照substr函数
@param$str要进行截取的字符串
@param$start要进行截取的开始位置,负数为反向截取
@param$end要进行截取的长度
*/
functionutf8_substr($str,$start=0){
if(empty($str)){
returnfalse;
}
if(function_exists('mb_substr')){
if(func_num_args()>=3){
$end=func_get_arg(2);
returnmb_substr($str,$start,$end,'utf-8');
}
else{
mb_internal_encoding("UTF-8");
returnmb_substr($str,$start);
}
}
else{
$null="";
preg_match_all("/./u",$str,$ar);
if(func_num_args()>=3){
$end=func_get_arg(2);
returnjoin($null,array_slice($ar[0],$start,$end));
}
else{
returnjoin($null,array_slice($ar[0],$start));
}
}
}
$str2='wo要截取zhongwen';
echo'<br/>';
echoutf8_substr($str2,0,-4);//returnwo要截取zhon
2,支持gb2312,gbk,utf-8,big5 中文截取方法
/*
* 中文截取,支持gb2312,gbk,utf-8,big5
*
* @param string $str 要截取的字串
* @param int $start 截取起始位置
* @param int $length 截取长度
* @param string $charset utf-8|gb2312|gbk|big5 编码
* @param $suffix 是否加尾缀
*/
public function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))
{
if(mb_strlen($str, $charset) <= $length) return $str;
$slice = mb_substr($str, $start, $length, $charset);
}
else
{
$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
preg_match_all($re[$charset], $str, $match);
if(count($match[0]) <= $length) return $str;
$slice = join("",array_slice($match[0], $start, $length));
}
if($suffix) return $slice."…";
return $slice;
}
❸ php 伪造 http_referer
function getContentCURL($url,$post_data = '',$user_agent="Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.8.1.3)", $header = 0, $follow_loc = 1, $cookie_file="/tmp/cookie.txt",$CURLOPT_TIMEOUT=30)
{
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
@curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
@curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
@curl_setopt($ch, CURLOPT_HEADER, $header);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow_loc);
@curl_setopt($ch, CURLOPT_TIMEOUT, $CURLOPT_TIMEOUT);
if (trim($post_data) != '') {
@curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$result = @curl_exec($ch);
@curl_close($ch);
return $result;
}
通过这种方式做代理, 你在仔细研究下吧
参考下
http://www.21andy.com/blog/20080507/1095.html
❹ php curl的几种用法
总结一下项目中用到curl的几种方式 1. php curl的默认调用方法,get方式访问url $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置http头 curl_setopt($ch, CURLOPT_ENCODING, "gzip" ); //设置为客户端支持gzip压缩 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30 ); //设置连接等待时间 curl_setopt($ch, CURLOPT_URL, $url ); curl_exec( $ch ); if ($error = curl_error($ch) ) {//出错处理return -1;}fclose($fp); $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //获取http返回值 if( $curl_code == 200 ) { //正常访问url}//异常 2. 设置http header支持curl访问lighttpd服务器Java代码$header[]= 'Expect:'; $header[]= 'Expect:'; 3. 设置curl,只获取http header,不获取body:Java代码curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); 或者只获取body:Java代码curl_setopt($ch, CURLOPT_HEADER, 0); // make sure we get the body curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_HEADER, 0); // make sure we get the body curl_setopt($ch, CURLOPT_NOBODY, 0); 4. 访问虚拟主机,需设置Host $header[]= 'Host: '.$host; 5. 使用post, put, delete等REStful方式访问urlpost:curl_setopt($ch, CURLOPT_POST, 1 ); put, delete: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); //或者PUT,需要服务器支持这些方法。6. 保存下载内容为文件
❺ php curl中CURLOPT_HTTPHEADER 这个参数的含义
php curl中CURLOPT_HTTPHEADER 这个参数的含义是:CURLOPT_HTTPHEADER 一个用来设置HTTP头字段的数组。Content-Type 表示后面的文档属于什么MIME类型。charset表示浏览器可接受的字符集。
HTTP头Servlet默认为text/plain,但通常需要显式地指定为text/html。由于经常要设置Content-Type,因此HttpServletResponse提供了一个专用的方法setContentType。
HTTP请求头的部分类型:
1、Accept:浏览器可接受的MIME类型。
2、Accept-Charset:浏览器可接受的字符集。
3、Accept-Encoding:浏览器能够进行解码的数据编码方式,比如gzip。Servlet能够向支持gzip的浏览器返回经gzip编码的HTML页面。许多情形下这可以减少5到10倍的下载时间。
4、Connection:表示是否需要持久连接。如果Servlet看到这里的值为“Keep-Alive”,或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时(例如Applet,图片),显着地减少下载所需要的时间。
5、Content-Length:表示请求消息正文的长度。
6、Cookie:这是最重要的请求头信息之一。
(5)php获取httpheader扩展阅读:
PHP中的CURL函数库(部分):
1、curl_setopt_array — 为cURL传输会话批量设置选项
2、curl_setopt — 设置一个cURL传输选项
3、curl_close — 关闭一个cURL会话
4、curl__handle — 复制一个cURL句柄和它的所有选项
5、curl_errno — 返回最后一次的错误号
6、curl_error — 返回一个保护当前会话最近一次错误的字符串
7、curl_escape — 使用 URL 编码给定的字符串
在实际的使用当中,使用得最多的函数是curl_setopt — 设置一个cURL传输选项说明:bool curl_setopt ( resource $ch , int $option , mixed $value )其中,ch 由 curl_init() 返回的 cURL 句柄。option 表示的是需要设置的CURLOPT_XXX选项。
option的可选参数:
1、CURLOPT_BUFFERSIZE 每次获取的数据中读入缓存的大小,但是不保证这个值每次都会被填满。在cURL 7.10中被加入。
2、CURLOPT_CLOSEPOLICY 不是CURLCLOSEPOLICY_LEAST_RECENTLY_USED就是CURLCLOSEPOLICY_OLDEST,还存在另外三个CURLCLOSEPOLICY_,但是cURL暂时还不支持。
3、CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为0,则无限等待。
❻ php 如何获取 客户端http header
<?php
$dir=$HTTP_GET_VARS["dir"];//.......取得上个页面传递来的路径
$file=$HTTP_GET_VARS["file"];//.......取得传递来的文件名
$url=parse_url($HTTP_REFERER);/*......取得前一页面的URL地址,并将其放入一个数组中*/
if($url[host]!=$HTTP_HOST){echo"要下载本软件请到<ahref=http://www.df365.org>东方小屋</a>";exit;}/*检查来源网站是不是自己的网站,如果不是,返回“要下载本……”*/
if(empty($dir))$dir="/";//......如果路径名为空,则为指定根目录
if(empty($file)){echo"未指定要下载的文件!";exit;}/*如果文件名为空,返回“未指定……”*/
$rootdir="文件存放的根目录";//......你的下载路径根目录
$realurl=$rootdir.$dir;//.......取得你的下载目录
chdir($realurl);//......将当前目录转到下载目录中
if(!file_exists($file)){echo"对不起,此链接已经失效,请在下载页面上向我们报告,谢谢!";exit;}//......测试文件是否存在
$filename=$file;
//发送文件头信息
header("Cache-control:private");//fixforIE
header("Content-Type:application/octet-stream");
header("Content-Length:".filesize($filename));
header("Content-Disposition:attachment;filename=$filename");
$fp=fopen($filename,'r');//以读取方式打开指定文件
fpassthru($fp);//**CORRECT**以二进制方式读取文件
fclose($fp);//关闭文件
?>
❼ PHP怎么获取表单提交的数据啊
一、用file_get_contents以get方式获取内容,需要输入内容为:
1、<?php
2、$url='http://www.domain.com/?para=123';
3、$html = file_get_contents($url);
4、echo $html;
5、?>
二、用file_get_contents函数,以post方式获取url,需要输入内容为
1、<?php
2、$url = 'http://www.domain.com/test.php?id=123';
3、$data = array ('foo' => 'bar');
4、$data = http_build_query($data);
5、$opts = array (
6、'http' => array (
7、 'method' => 'POST',
8、 'header'=> "Content-type: application/x-www-form-urlencoded " .
9、 "Content-Length: " . strlen($data) . " ",
10、 'content' => $data
11、)
12、);
13、$ctx = stream_context_create($opts);
14、$html = @file_get_contents($url,'',$ctx);
15、?>
三、用fopen打开url,以get方式获取内容,需要输入内容为
1、<?php
2、$fp = fopen($url, 'r');
3、$header = stream_get_meta_data($fp);//获取信息
4、while(!feof($fp)) {
5、$result .= fgets($fp, 1024);
6、}
7、echo "url header: {$header} <br>":
8、echo "url body: $result";
9、fclose($fp);
10、?>
四、用fopen打开url,以post方式获取内容,需要输入内容为
1、<?php
2、$data = array ('foo2' => 'bar2','foo3'=>'bar3');
3、$data = http_build_query($data);
4、$opts = array (
5、'http' => array (
6、'method' => 'POST',
7、'header'=> "Content-type: application/x-www-form-urlencoded Cookie:cook1=c3;cook2=c4 " .
8、"Content-Length: " . strlen($data) . " ",
9、'content' => $data
10、)
11、);
12、$context = stream_context_create($opts);
13、$html = fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb' ,false, $context);
14、$w=fread($html,1024);
15、echo $w;
16、?>
五、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,需要输入内容为
1、?php
2、function get_url ($url,$cookie=false)
3、{
4、$url = parse_url($url);
5、$query = $url[path]."?".$url[query];
6、echo "Query:".$query;
7、$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
8、if (!$fp) {
9、return false;
10、} else {
11、$request = "GET $query HTTP/1.1 ";
12、$request .= "Host: $url[host] ";
13、$request .= "Connection: Close ";
14、if($cookie) $request.="Cookie: $cookie ";
15、$request.=" ";
16、fwrite($fp,$request);
17、while(!@feof($fp)) {
18、$result .= @fgets($fp, 1024);
19、}
20、fclose($fp);
21、return $result;
22、}
23、}
24、//获取url的html部分,去掉header
25、function GetUrlHTML($url,$cookie=false)
26、{
27、$rowdata = get_url($url,$cookie);
28、if($rowdata)
29、{
30、$body= stristr($rowdata," ");
31、$body=substr($body,4,strlen($body));
32、return $body;
33、}
34、 return false;
35、}
36、?>