Home  >  Article  >  Backend Development  >  PHP stream_context_create() function and usage analysis_PHP tutorial

PHP stream_context_create() function and usage analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:31:28740browse

Function: Create and return a text data stream and apply various options. It can be used for special processes such as timeout settings, proxy servers, request methods, and header information settings for processes such as fopen() and file_get_contents().
Function prototype: resource stream_context_create ([ array $options [, array $params ]] )
Usage
Example 1:

Copy Code The code is as follows:

$opts = array( 'http-->array(
'method'=>"GET ",
'header'=>"Accept-language: enrn" .
"Cookie: foo=barrn"
)
);
$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);
?>

Example 2:
Copy code The code is as follows:

$opts = array( 'http-->array(
'method'=>"GET",
'header'=>"Accept-language: enrn" .
"Cookie: foo=barrn"
)
);
$context = stream_context_create($opts);
?>
You would setup the header this way:
$opts = array( 'http-->array(
'method'=>"GET",
'header'=>array("Accept-language: en",
"Cookie: foo=bar",
"Custom-Header : value")
)
);
$context = stream_context_create($opts);
?>

Example 3:
Copy code The code is as follows:

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

Example 4:
Copy code The code is as follows:

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 .= "--$boundaryn ";
$data .= "Content-Disposition: form-data; name="".$key.""nn".$val."n";
}
$data .= " --$boundaryn";
//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/jpegn";
$data .= "Content-Transfer-Encoding: binarynn";
$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);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322991.htmlTechArticleFunction: Create and return a text data stream and apply various options, which can be used for fopen(), file_get_contents() The timeout setting, proxy server, request method, header information setting of other processes...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn