1.在file_get_contents和fopen作为参数调用。

网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站设计,高端网页制作,对成都活动板房等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业成都网站营销优化,H5建站,响应式网站。
resource fopen( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
string file_get_contents( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1[, int $maxlen ]]]] )
注意这两个函数都有一个context参数,类型是resource
而context是怎么整理并放置进去的呢? 简单的案例
如下:
$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.example.com   
   with additional headers shown above */    
$fp = fopen('http://www.example.com', 'r', false, $context);    
fpassthru($fp);    
fclose($fp);    
?>
具体的用法就是将一个数组形式的参数列表用create方法创建,即可在fopen和file_get_contents中使用了。
以下是官方的解释:
PHP 提供了多种上下文选项和参数,可用于所有的文件系统或数据流封装协议。上下文(Context)由stream_context_create() 创建。选项可通过 stream_context_set_option() 设置,参数可通过stream_context_set_params() 设置。
简单例:
$options = [    
    "socket" =>[    
        "bindto"=>"0:7000", //”127.0.0.120:7001”    
    ],    
];    
$context = stream_context_create($options);    
echo file_get_contents("http://www.baidu.com",false,$context);    
?>
这里是以本机7000端口去访问获取百度的首页内容。
例3获取一个页面并发送POST数据:
$postdata = http_build_query(    
    array(    
'var1' => 'some content',    
'var2' => 'doh'    
)    
);    
$opts = array('http' =>    
    array(    
'method'  => 'POST',    
'header'  => 'Content-type: application/x-www-form-urlencoded',    
'content' => $postdata    
)    
);    
$context = stream_context_create($opts);    
$result = file_get_contents('http://example.com/submit.php', false, $context);    
?>
例4   
$data = array ('foo' => 'bar');    
$data = http_build_query($data);    
$opts = array (    
     'http' => array (    
         'method' => 'POST',    
         'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .    
                    "Content-Length: " . strlen($data) . "\r\n",    
         'content' => $data    
     ),    
);    
$context = stream_context_create($opts);    
$html = file_get_contents('http://www.example.com', false, $context);    
echo $html;    
?>
本章对context的讲解主要用于各种协议内容的获取,POST GET 和相关的设置,包含curl,file_get_contents,fopen,soket,还有各种http https ftp ssl 等。 具体在实际运用中再详细研究。
网站名称:复习PHP-语言参考-Context选项和参数
本文网址:http://www.scyingshan.cn/article/jgegsd.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 