我正在尝试使用curl访问受保护的站点(.htpasswd),以检查其是否可访问并返回类似200或302的代码。
我尝试使用错误的密码和用户名访问它。我不想登录,我只想检查它是否在线/可访问。
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "aaa:bbb");
Run Code Online (Sandbox Code Playgroud)
如何通过curl获得401代码?
我正在像这样检查http代码,但是在htpasswd中,它为空
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE),true
Run Code Online (Sandbox Code Playgroud)
先感谢您!
如果在curl_getinfo函数中使用CURLINFO_HTTP_CODE属性,则可以从curl中获取状态代码,如下所示:
<?php
$URL = 'http://yourdomain.com/protected_dir';
$Auth_Username = "username_here";
$Auth_Password = "password_here";
//make curl request
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERPWD, "{$Auth_Username}:{$Auth_Password}");
$Output = curl_exec($ch);
//get http code
$HTTP_Code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //this is important to get the http code.
curl_close($ch);
echo 'HTTP code: ' . $HTTP_Code;
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
901 次 |
| 最近记录: |