1、如果你是将你的上面代码放到服务器运行的话,百度的服务器可能会识别你USERAGENT为机器人robot,拒绝你的访问
2、file_get_contents()某些时候是可行的,但是有些特殊情况也可以使用curl库的函数,为了防止对方识别你为robot,可以将服务器伪装访问的浏览器用户是Mozilla/4.0,比如以下代码
$curl = curl_init();
//这里设置你的USERAGENT
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
$xxx = curl_exec($curl);
curl_close ($curl);
echo $xxx;
?>
补充:
关于你的问题,这段代码我是运行成功的
$url = "http://www.baidu.com";
$page = "/services/calculation";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}
?>