Heim  >  Artikel  >  php教程  >  PHP file_get_contents 简单函数

PHP file_get_contents 简单函数

WBOY
WBOYOriginal
2016-08-04 08:53:381423Durchsuche
跳至 [1] [全屏预览]
/**
 * file_get_contents 函数
 * @param string $url @要访问的URL
 * @param array $post_data @请求的数据(数组)
 * @param string $method @请求方式 默认 post
 * @return bool|string
 */
function file_contents_post($url = '', $post_data = array(), $method = 'POST')
{
	$sFile = false;
	if(!empty($post_data) && !empty($url)){
		$data = http_build_query($post_data);
		$opts = array(
			'http'=>array(
				'method' => $method,
				'header' => "Content-type: application/x-www-form-urlencoded\r\n".
					"Content-length: ".strlen($data)."\r\n" .
					"Cookie: foo=bar\r\n" .
					"\r\n",
				'content' => $data
			)
		);
		$cxContext = stream_context_create($opts);
		$sFile = file_get_contents($url, false, $cxContext);
	}

	return $sFile;
}
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn