PHP Function Reference

PHP stream_context_get_default() Function



The PHP stream_context_get_default() function returns the default stream context which is used whenever file operations like fopen(), file_get_contents(), etc. are called without a context parameter. Options for the default context can optionally be specified with this function using the same syntax as stream_context_create() function.

Syntax

stream_context_get_default(options)

Parameters

options Required. Specify the context options. It must be an associative array of associative arrays in the format $arr['wrapper']['option'] = $value.

Return Value

Returns a stream context resource.

Example: using stream_context_get_default()

The example below shows the usage of stream_context_get_default() function.

<?php
//default options
$default_opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar",
  )
);

//creating default context resource
$default = stream_context_get_default($default_opts);

//sending a GET request to www.example.com using 
//context options specified in $default_opts
readfile('http://www.example.com');
?>

The output of the above code will be similar to:

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", 
                     "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

❮ PHP Streams Reference