嗯 1. [代码] [PHP]代码 /** * file_get_contents 函数 * @param string $url @要访问的URL * @param array $post_data @请求的数据(数组) * @param string $method @请求方式 默认 post * @param string $content_type * @retur
1. [代码][PHP]代码
/**
* file_get_contents 函数
* @param string $url @要访问的URL
* @param array $post_data @请求的数据(数组)
* @param string $method @请求方式 默认 post
* @param string $content_type
* @return bool|string
*/
function file_contents_post($url = '', $post_data = array(), $method = 'POST', $content_type = 'form')
{
$sFile = false;
if(!empty($url)){
$type = [
'form' => "application/x-www-form-urlencoded",
'json' => "application/json",
];
$ua = $_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; 4399Box.560; .NET4.0C; .NET4.0E)";
$data = http_build_query($post_data);
$opts = [
'http' => [
'method' => $method,
'header' =>
"Content-type: {$type[$content_type]}\r\n".
"User-Agent: {$ua}\r\n". //模拟UA
"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;
}
