试试下面的方法(来自 How to use basic authorization in PHP curl )
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, 'http://192.168.1.0:50005/status'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_USERPWD, "auth:auth"); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents;
他是一个auth的验证串 你说的好像不行 返回的是无效参数400
@锦瑟_华年:
改为 curl_setopt($ch, CURLOPT_USERPWD, "auth"); 试试
@dudu: 不行
$ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, 'http://192.168.1.0:50005/status'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_USERPWD, "asdasdasdasd"); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents;
@锦瑟_华年:
加上Bearer试试:
curl_setopt($ch, CURLOPT_USERPWD, "Bearer auth");
@dudu: 一直返回的是无效参数 是不是格式上的错误 我在试试
@锦瑟_华年: 试试
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: auth'));
@dudu: curl_setopt($ch, CURLOPT_HTTPHEADER, array('auth: '.$res['auth']));
可以了 谢谢了
@锦瑟_华年: 您好。请问您是怎么实现的。我也遇到这样的问题了。我的是AUTH_DIGEST认证
$curl=curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_USERNAME,"admin");
curl_setopt($curl,CURLOPT_PASSWORD,"XUxing6666");
curl_setopt($curl,CURLOPT_HTTPAUTH,CURLAUTH_DIGEST);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
curl_setopt($curl, CURLOPT_HTTPHEADER, array('auth: '.'http://envir-gis-test.sdxxtop.com/api/index/ceshi'));
$data=curl_exec($curl);
echo $data;