php curl couldn't resolve host,php中CURL报Could not resolve host错误

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);

}

?>

d11064dcc590fae550306d64f86035d5.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值