php学习php://input输入流和curl

<?php
/*
//@time 9:26
//
file_get_contents和curl的区别/
+-------------------------------------------------------------------------------+
file_get_contents其实是内置的文件操作的函数的合并版本,比如file_exists,fopen,fread,fcose,专门给懒人提供的.而他主要是对付本地文件的,同时又加入了对网络文件的支持

curl是专门用来进行网络交互的库,提供了一堆自定义选项,用来对应不同的环境.稳定性大于file_get_contents

+--------------------------------------------------------------------------------+
php手册:

file_get_contents:将整个文件读入一个字符串

file_get_contents(string $filename[,bool $use_include_path[,resource $context[,int $offset [,int $maxlen]]]]);


和file()一样都是将文件读入到一个字符串里面.将在参数offset所指定的位置开始读取长度为maxlen的内容,如果失败,file_get_contents()将返回FALSE.


+--------------------------------------------------------------------------------+
php有三种post数据,分别为Curl,socket,file_get_contents:

1/表单提交方式
$_POST和php://input可以去得到值,$HTTP_RAW_POST_DATA为空
$_POST以关联数组方式组织提交的数据,并对此进行编码处理,例如urldecode,甚至是转码替换
php://input可通过输入流以文件读取方式取得未经处理的POST原始数据.


PHP输入流php://input
在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组.

php:input allows you to read raw POST data.It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php:// is not available with enctype="multipart/form-data"
翻译过来:
php://input可以读取没有处理过的POST数据,相对于$HTTP_RAW_DATA而言,他给内存带来的压力最小,而且不需要特殊的php.ini设置.php://input不能用于enctype=multipart/form-data

读取POST数据:
PHPer一定熟悉$_POST,$_POST与PHP存在的关联与区别:


2.fsockopen提交POST数据下PHP获取POST获取
$sock=fsockopen("localhost",80,
 $errno,$errstr,30);
if(!$sock) die("$errstr($errno)\n");
$data="txt=".urlencode("中")."&bar=".urlencode("Value for Bar");
fwrite($sock,"POST/posttest/response.php HTTP/1.0\r\n");
fwrite($sock,"Content-type:application/x-www-form-urlencoded\r\n");
fwrite($sock,"Content-length:".
 strlen($data)."\r\n");
fwrite($sock,"Accept:*/*\r\n");
fwrite($sock,"\r\n");
fwrite($sock,"\r\n");
fwrite($sock,"$data\r\n");
fwrite($sock,"\r\n");
$headers="";
while($str=trim(fgets($sock,4096)))
$headers.="$str\n";
echo "\n";
$body="";
while(!feof($sock))
 $body.=fgets($sock,4096);
fclose($sock);
echo $body;

POST获取POST数据结论:
1.用php://input可以很便捷的取到原始的POST数据
2.$HTTP_RAW_POST_DATA仅在POST的Content-Type类型不为PHP识别时才有效
如通常通过页面表单提交后的POST数据,不能通过$HTTP_RAW_POST_DATA提取到.因其编码属性(enctype属性的)如通常通过页面表单提交后的POST数据,

3.$_POST仅当数据按照application/x-www-form-urlencoded类型提交时才能实现PHP获取POST数据.


当php://input碰到了multipart/form-data

<form enctype="multipart/form-data" action="1.php"/>
 <input type="text" name="n"/>
 <input type="file" name="f"/>
 <input type="submit" value="upload now"/>
</form>

http://input和$HTTP_RAW_POST_DATA是相同的!!!!

+--------------------------------------------------------------------------------+
问题:
1/如何获取cookie给curl中使用?
curl一个网站页面,但是目标站的设置存放在cookie中的(这个cookie值是固定的),必须指定cookie才能正常显示某些内容!!!

php curl lib中,设置一个cookie文件 $cookieFilePath
在初始化以后和执行页面以前的调用:
curl_setopt($c,CURLOPT_COOKIEFILEM,$cookieFilePath);
curl_setopt($c,CURLOPT_COOKIEJAR,$cookieFilePath);

最好在使用前进行清除
@unlink($cookieFilePath);

unlink---删除文件
删除filename和unix C的unlink()函数相似.成功时返回TRUE,或者失败时返回FALSE.
删除目录函数rmdir()


一般cookie模拟登录,应该是你想要的:

curl的封装程序见这个问题@求php实现POST请求}@
//获取页面的cookie
$http=new HttpRequest;
$http->url='目标网站的页面';
$response=$http->get();
$header=$response->headers;
//真正请求开始
$http->url="访问地址";
if(isset($header['Set-Cookie']) && is_array($header['Set-Cookie'])){
 $http->cookie=$header['Set-Cookie'];
}
$response=$http->get();
$body=$response->body;


 

posted @ 2012-09-29 15:50  sgsheg  阅读(1238)  评论(0编辑  收藏  举报