Maison  >  Article  >  php教程  >  PHP file_get_contents 简单函数

PHP file_get_contents 简单函数

WBOY
WBOYoriginal
2016-08-04 08:53:381423parcourir
跳至 [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;
}
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn