PHP stream

作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
函数原型:resource stream_context_create ([ array $options [, array $params ]] )
用法
例子一:

复制代码 代码如下:

<?php
$opts = array( 'http-->array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.jb51.net
with additional headers shown above */
$fp = fopen('http://www.jb51.net', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

例子二:

复制代码 代码如下:

<?php
$opts = array( 'http-->array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
?>
You would setup the header this way:
<?php
$opts = array( 'http-->array(
'method'=>"GET",
'header'=>array("Accept-language: en",
"Cookie: foo=bar",
"Custom-Header: value")
)
);
$context = stream_context_create($opts);
?>

例子三:
复制代码 代码如下:

<?php
$opts = array('http' => array('proxy' => 'tcp://127.0.0.1:8080', 'request_fulluri' => true));
$context = stream_context_create($opts);
$data = file_get_contents('http://www.jb51.net', false, $context);
echo $data;
?>

例子四:

复制代码 代码如下:

<?php
function do_post_request($url, $postdata, $files = null)
{
$data = "";
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);
//Collect Postdata
foreach($postdata as $key => $val)
{
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
}
$data .= "--$boundary\n";
//Collect Filedata
foreach($files as $key => $file)
{
$fileContents = file_get_contents($file['tmp_name']);
$data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n";
$data .= "Content-Type: image/jpeg\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents."\n";
$data .= "--$boundary--\n";
}
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
'content' => $data
));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
//set data (in this example from post)
//sample data
$postdata = array(
'name' => $_POST['name'],
'age' => $_POST['age'],
'sex' => $_POST['sex']
);
//sample image
$files['image'] = $_FILES['image'];
do_post_request("http://www.jb51.net", $postdata, $files);
?>

时间: 2024-08-03 15:06:24

PHP stream的相关文章

用stream直接下载文件

stream|下载 在浏览器的地址栏里直接输入一个doc或xls或jpg的文件的url路径,那么该文件会直接显示在浏览器里.而在很多时候我们希望能直接弹出下载提示框让用户下载,我们该怎么办呢?这里有两种方法:1.设置你的服务器的iis,给doc等后缀名做映射2.在向客户端发送时设置其contenttype 下面详细说明方法2 <%Response.Buffer=trueResponse.Clear dimurlDimfso,fl,flsizedimDnameDimobjStream,Conten

stream-Darwin Stream Server 补丁下载

问题描述 Darwin Stream Server 补丁下载 Linux 64位机安装Darwi Stream Server需要的补丁现在下载不了了. http://dss.macosforge.org/trac/raw-attachment/ticket/6/dss-6.0.3.patchhttp://dss.macosforge.org/trac/raw-attachment/ticket/6/dss-hh-20080728-1.patchhttp://dss.macosforge.org/

php错误提示failed to open stream: HTTP request failed!的完美解决方法

google或者baidu一下,好多这样的问题,解决的方法都是修改php.ini,把allow_url_fopen给启用,改成 allow_url_fopen = On 这样做可以解决某些人的问题,有人说在php.ini中,有这样两个选项:allow_url_fopen =on(表示可以通过url打开远程文件),user_agent="PHP"(表示通过哪种脚本访问网络,默认前面有个 " ; " 去掉即可.)重启服务器. 但是有些还是会有这个警告信息,想用完美的解决

java8 如何进行stream reduce,collection操作

一.概念介绍     在java8 JDK包含许多聚合操作(如平均值,总和,最小,最大,和计数),返回一个计算流stream的聚合结果.这些聚合操作被称为聚合操作.JDK除返回单个值的聚合操作外,还有很多聚合操作返回一个collection集合实例.很多的reduce操作执行特定的任务,如求平均值或按类别分组元素.     JDK提供的通用的聚合操作:Stream.reduce,Stream.collection     注意:本文将reduction operations翻译为聚合操作,因为r

用stream读文件

stream|stream 前几天要写一个东西里面有用到读文件的. 可是我不想用FSO,我怕有的空间不支持.可是网络上找了半天没有找到一个不是用FSO写的. 还是自己做了一个. 我记得以前在做无组件上传,并保存文件的时候有用到stream我发现有一个LoadFromFile的方法.可以读取文件.下面是我的代码. function readfile(URL,chartype) set srmObj = server.CreateObject("adodb.stream") srmObj.

关于Adodb.Stream 的使用说明

ado|stream|ado|stream 组件:"Adodb.Stream"有下列方法:Cancel 方法 使用方法如下 Object.Cancel 说明:取消执行挂起的异步 Execute 或 Open 方法的调用.Close 方法 使用方法如下 Object.Close :关闭对像CopyTo 方法 使用方法如下 Object.CopyTo(destStream,[CharNumber]) 说明:将对像的数据复制,destStream指向要复制的对像,CharNumber为可选参

Stream Tokenizing(分解字符串)

stream|字符串 从sun网站看到的Stream TokenizingIn Tech Tips: June 23, 1998, an example of string tokenization was presented, using the class java.util.StringTokenizer. There's also another way to do tokenization, using java.io.StreamTokenizer. StreamTokenizer

ASP有函数可以把某个网页通过STREAM下载

stream|函数|网页|下载|stream|函数|下载 <script language="vbscript">   Function bytes2BSTR(vIn)      Dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode   strReturn = ""      For i = 1 To LenB(vIn)    ThisCharCode = AscB(MidB(vI

关于Adodb.Stream的写数据库数据到客户端

ado|stream|客户端|数据|数据库 这是一个从服务器数据库直接下数据到客户端文件的例子,文件名和扩展名可以根据需要随便写.new/temp.htm要求真实存在,其实没有什么作用,只是引导一下,否则不能对stram数据进行写操作. 网络上流传的这方面的东西不少,但多数是copy来的,很简单的事情搞得那么复杂.真正经过实践的东东少得可怜.于是自己写了一个,虽然简单,也有些问题,但是copy下去就可以用了.有问题的朋友可以跟我联系,好一起学习. filemystream=server.mapp

ASP中adodb Stream组件的详细用法

ado|stream 有下列方法:Cancel 方法     使用方法如下     Object.Cancel     说明:取消执行挂起的异步 Execute 或 Open 方法的调用.Close  方法     使用方法如下     Object.Close     :关闭对像CopyTo 方法     使用方法如下     Object.CopyTo(destStream,[CharNumber])     说明:将对像的数据复制,destStream指向要复制的对像,CharNumber