PHP?stream_context_create()的作用是什么

這篇文章主要介紹“PHP stream_context_create()的作用是什么”,在日常操作中,相信很多人在PHP stream_context_create()的作用是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PHP stream_context_create()的作用是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)公司10多年成都定制網(wǎng)站服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),成都定制網(wǎng)站及推廣,對茶樓設(shè)計等多個行業(yè)擁有多年的網(wǎng)站設(shè)計經(jīng)驗的網(wǎng)站建設(shè)公司。

創(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);

 $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);
 ?>

到此,關(guān)于“PHP stream_context_create()的作用是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

文章標(biāo)題:PHP?stream_context_create()的作用是什么
文章來源:http://muchs.cn/article38/ghgjpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司網(wǎng)站營銷、定制開發(fā)、搜索引擎優(yōu)化、云服務(wù)器、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)