stream_context_create() - php 流数据函数stream
stream_context_create()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
创建资源流上下文
说明
stream_context_create([array $options[,array $params]]): resource创建并返回一个资源流上下文,该资源流中包含了$options中提前设定的所有参数的值。
参数
$options必须是一个二维关联数组,格式如下:$arr['wrapper']['option']=$value。
默认是一个空数组。
$params必须是$arr['parameter']=$value格式的关联数组。请参考context parameters里的标准资源流参数列表。
返回值
上下文资源流,类型为resource。
更新日志
版本 | 说明 |
---|---|
5.3.0 | 增加了可选参数$params。 |
范例
使用stream_context_create()
参见
stream_context_set_option()
对资源流、数据包或者上下文设置参数- Listing of supported wrappers (支持的协议和封装协议)
- Context options (上下文(Context)选项和参数)
Something to keep in mind when creating SSL streams (using https://): One would think - the proper way to create a stream options array, would be as follows: THAT IS THE WRONG WAY!!! Take notice to the 3rd line: 'https' => array ( The CORRECT way, is as follows: Notice, the NEW 3rd line: 'http' => array ( Now - keep this in mind - I spent several hours trying to trouble shoot my issue, when I finally stumbled upon this non-documented issue. The complete code to post to a secure page is as follows:
I big NOTE that i hope will help some one. Something that is not mentioned in the documentation, is that when php is compiled --with-curlwrappers, So, instead of: You would setup the header this way: This will work.
I spent a good five hours trying to figure this out, so hopefully it will save someone else some time. When you are trying to download a file via ftp through an HTTP proxy note that the following will not be enough: Your proxy will respond that authentication is required. You may scratch your head and think "but I'm providing authentication!" The issue is that the 'header' value is only applicable to http connections. So to authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for using on FTP. It's a bit roundabout, but it works. Note that the 'header' val in the ftp array is redundant, but I kept it in anyway.
When using the https protocol you'll have to make sure to set the right context options to use the full "power" of the ssl/tls encryption. More information about those context options can be found at http://php.net/manual/en/context.ssl.php
I found the following code worked for me for POSTing some binary data to a remote server. I am putting it here since I could not find a quick solution to this by 'googling' or looking through this documentation. Disclaimer: I have no idea if this a 'good' solution, since I'm new to PHP, but it may just suit your needs as it did mine. I am assuming bad things will happen with very large files since the entire file is read into $fileContents. I am using PHP 5.2.8. $fileHandle = fopen("someImage.jpg", "rb"); $fileContents = stream_get_contents($fileHandle); fclose($fileHandle); $params = array( 'http' => array ( 'method' => 'POST', 'header'=>"Content-Type: multipart/form-data\r\n", 'content' => $fileContents ) ); $url = "http://somesite.somecompany.com?someParam=someValue"; $ctx = stream_context_create($params); $fp = fopen($url, 'rb', false, $ctx); $response = stream_get_contents($fp);
Hi,you can create an array of parameters(what it's called a stream context),which can be transmitted each time you read or write a stream through a socket.In the below example: $opts =array('http'=>arra('method'=>"GET", 'header'=>"Accept-language:en\r\n"."Cookie: foo=bar\r\n"); What you're actually doing is create a set of parameters(the protocol to be used,the request method,additional http headers and a cookie) which will be used each time you open a socket connection to request www.example.com.This saves a lot of time if you want to use these parameters (called a stream context) whenever you include them when making a request to www.example.com,instead of having to specify them over and over again. Using the previous example,say you want to create a stream context,which sends a "Content-Type" http header and utilize it when making a request to www.example.com.Take a look: $opts = array('http'=>array('method'=>"GET", 'header'=>"Content-Type: text/xml; charset=utf-8"); $context = stream_context_create($opts); $fp = fopen('http://www.example.com','r',false,$context); fpassthru($fp); fclose($fp); Now,when you make a request to www.example.com,the above http header will be included within the socket and transmitted to the server.Best of luck for you friends,Hossein
In addition to the context options mentioned above (appendix N), lower down context options for sockets can be found in appendix P - http://www.php.net/manual/en/transports.php
In some cases, set a header option as an array, and not a string, depending on server configuration.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)