PHP stream_context_create()作用和用法分析
2015-01-24信息快讯网
创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
作用:创建并返回一个文本数据流并应用各种选项,可用于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);
?>
UCenter 批量添加用户的php代码
PHP imagecreatefrombmp 从BMP文件或URL新建一图像
php学习之function的用法
PHP中防止直接访问或查看或下载config.php文件的方法
php中配置文件操作 如config.php文件的读取修改等操作
PHP连接SQLServer2005的实现方法(附ntwdblib.dll下载)
php.ini-dist 和 php.ini-recommended 的区别介绍(方便开发与安全的朋友)
apache+codeigniter 通过.htcaccess做动态二级域名解析
PHP sprintf() 函数的应用(定义和用法)
深入了解 register_globals (附register_globals=off 网站打不开的解决方法)
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
php中jQuery插件autocomplate的简单使用笔记
header中Content-Disposition的作用与使用方法
PHP Parse Error: syntax error, unexpected $end 错误的解决办法
The specified CGI application misbehaved by not returning a complete set of HTTP headers
php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)
PHP 5.3 下载时 VC9、VC6、Thread Safe、Non Thread Safe的区别分析
php expects parameter 1 to be resource, array given 错误
让PHP COOKIE立即生效,不用刷新就可以使用
在MongoDB中模拟Auto Increment的php代码
php SQL Injection with MySQL
PHP下通过file_get_contents的代理使用方法
PHP Session_Regenerate_ID函数双释放内存破坏漏洞
php的日期处理函数及uchome的function_coomon中日期处理函数的研究
php simplexmlElement操作xml的命名空间实现代码
php array_unique之后json_encode需要注意
php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
浅谈PHP Extension的开发――基础篇第1/2页