PHP stream_context_create()作用和用法分析
更新時間:2011年03月29日 16:32:15 作者:
創(chuàng)建并返回一個文本數(shù)據(jù)流并應(yīng)用各種選項,可用于fopen(),file_get_contents()等過程的超時設(shè)置、代理服務(wù)器、請求方式、頭信息設(shè)置的特殊過程。
作用:創(chuàng)建并返回一個文本數(shù)據(jù)流并應(yīng)用各種選項,可用于fopen(),file_get_contents()等過程的超時設(shè)置、代理服務(wù)器、請求方式、頭信息設(shè)置的特殊過程。
函數(shù)原型: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.dbjr.com.cn
with additional headers shown above */
$fp = fopen('http://www.dbjr.com.cn', '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.dbjr.com.cn', 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.dbjr.com.cn", $postdata, $files);
?>
函數(shù)原型:resource stream_context_create ([ array $options [, array $params ]] )
用法
例子一:
復(fù)制代碼 代碼如下:
<?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.dbjr.com.cn
with additional headers shown above */
$fp = fopen('http://www.dbjr.com.cn', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>
例子二:
復(fù)制代碼 代碼如下:
<?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);
?>
例子三:
復(fù)制代碼 代碼如下:
<?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.dbjr.com.cn', false, $context);
echo $data;
?>
例子四:
復(fù)制代碼 代碼如下:
<?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.dbjr.com.cn", $postdata, $files);
?>
相關(guān)文章
redis+php實現(xiàn)微博(三)微博列表功能詳解
這篇文章主要介紹了redis+php實現(xiàn)微博列表功能,結(jié)合實例形式分析了php+redis獲取微博關(guān)注人列表及微博發(fā)布信息列表的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09自寫的利用PDO對mysql數(shù)據(jù)庫增刪改查操作類
這篇文章主要給大家介紹了關(guān)于自寫的利用PDO對mysql數(shù)據(jù)庫的增刪改查操作類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02