file_get_contents函数高级应用

file_get_contents()高级应用

函数原型:
file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource $context [, int $offset = 0 [, int $maxlen ]]]] ) : string

1、读取文件内容

2、发起简单get请求

3、发起带上下文的请求
	$string = file_get_contents($url, false, $context);

	重点就是 $context

	$context = stream_context_create($options);

	而 $options 的元素如下:
	$postData = array();
	$auth = base64_encode("$user:$password");

	$options['http'] = [
		'method'=>"POST",
	    'header'=>"Accept-language: en\r\n" .
	              "Cookie: foo=bar\r\n".
	              "Authorization: Basic $auth\r\n" // 可以带上 Basic Authorization 认证
	              ,
	    'content'=>http_build_query($postData),
	    // 可设置代理服务器
	    'proxy'=>'tcp://127.0.0.1:8080',
	    'request_fulluri' => true
	];

	如果需要加上ssl
	$options['ssl'】 => array(
        'verify_peer'   => true,
        'cafile'        => __DIR__ . '/cacert.pem',
        'verify_depth'  => 5,
        'CN_match'      => 'secure.example.com'
    )

猜你喜欢

转载自blog.csdn.net/raoxiaoya/article/details/92832428