little_monste
2016-01-24 12:31
我的只有加上这个好使$curlobj,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
为什么我加上你的那个不好使,老是提示HTTP_USER_AGENT下标未定义啊Undefined index
<?php $data = 'theCityName=深圳'; $curlobj = curl_init(); curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"); curl_setopt($curlobj, CURLOPT_HEADER, 0); curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlobj, CURLOPT_POST, 1); curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data); curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data) )); curl_setopt($curlobj, CURLOPT_USERAGENT, "user-agent:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0"); //浏览器头信息 $rtn = curl_exec($curlobj); if(!curl_errno($curlobj)){ echo $rtn; } else { echo 'Curl error: ' . curl_error($curlobj); } curl_close($curlobj); ?>
你先打印 echo $_SERVER['HTTP_USER_AGENT'];这个看看,可能不是Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
解决方法:curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
<?php $data = 'theCityCode=2013&theUserID='; $curlobj = curl_init(); curl_setopt($curlobj,CURLOPT_URL,'http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather'); //设置访问的URL curl_setopt($curlobj,CURLOPT_HEADER,0); //将头文件的信息作为数据流输出 curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,1); //执行后不直接打印出来 curl_setopt($curlobj,CURLOPT_POST,1); //发送POST请求 curl_setopt($curlobj,CURLOPT_POSTFIELDS,$data); //发送POST请求参数 curl_setopt($curlobj,CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf-8","Content-Length: ".strlen($data))); //发送header头部信息 curl_setopt($curlobj, CURLOPT_USERAGENT, "user-agent:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0"); //浏览器头信息 $rtn = curl_exec($curlobj); if(!curl_errno($curlobj)){ echo $rtn; } else { echo 'cURL error: '.curl_error($curlobj); } curl_close($curlobj);
这个可以用。
PHP中的数据传输神器cURL
34277 学习 · 218 问题
相似问题