PHP stream_context_create()作用和用法分析

yipeiwu_com5年前PHP代码库
作用:创建并返回一个文本数据流并应用各种选项,可用于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('//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('//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("//www.jb51.net", $postdata, $files);
?>

相关文章

php中file_get_contents与curl性能比较分析

php中file_get_contents与curl性能比较分析

本文实例讲述了php中file_get_contents与curl性能比较分析。分享给大家供大家参考。具体如下: 在php中如果不仔细的去分析性能会发现file_get_contents...

php内存缓存实现方法

本文实例讲述了php内存缓存实现方法。分享给大家供大家参考。具体如下: 在php中缓存分为很多种类型如,内存缓存,文件缓存,页面缓存。本文要来讲述关于php中内存缓存的一些方法,这里我们...

最令PHP初学者们头痛的十四个问题

【1】页面之间无法传递变量 get,post,session在最新的PHP版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用$_GET['foo'],$_PO...

php中unserialize返回false的解决方法

本文实例讲述了php中unserialize返回false的解决方法,分享给大家供大家参考。具体方法如下: php 提供serialize(序列化) 与unserialize(反序列化)...

一个简单php扩展介绍与开发教程

一个简单php扩展介绍与开发教程

我们使用php扩展,主要目的是提高程序的执行效率,对于访问量很大的代码或者逻辑将其写成扩展。在做项目的过程中,需要对数据进行排序,数据运算比较复杂;我们准备对一百万个数据进行排序, 下面...