php访问带基本认证的页面
$host = '192.168.2.1'; //主机 $path = '/status.htm'; //路径 $user = 'admin'; //用户名 $pass = 'admin'; //密码 $queryString = '?a=1&b=2'; //query字串 //方法一,使用fsockopen $authorizationToken = base64_encode("{$user}:{$pass}"); $fp = fsockopen('tcp://' . $host, 80, $errorNo, $errorMsg, 45); //$fp = fsockopen('ssl://' . $host, 443, $errorNo, $errorMsg, 45); if (!$fp) { echo 'error: ', $errorNo, ' ', $errorMsg; } else { $header = "GET {$path}{$queryString} HTTP/1.1\r\n"; $header .= "Host: {$host}\r\n"; //$header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; $header .= "Authorization: Basic {$authorizationToken}\r\n"; //认证 $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-length: " . strlen($queryString) . "\r\n"; $header .= "Connection: Close\r\n\r\n"; fwrite($fp, $header); $return = ''; while (!feof($fp)) { $return .= fgets($fp, 4096); } fclose($fp); echo $return; } //方法二,使用curl $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://{$host}{$path}{$queryString}"); curl_setopt($ch, CURLOPT_PORT, 443); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$pass}"); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); $return = curl_exec($ch); if (!$return) { echo curl_error($ch); } else { echo $return; } curl_close($ch);
相关推荐
83911535 2020-11-13
曾是土木人 2020-10-31
yegen00 2020-10-21
soralaro 2020-10-11
katanaFlower 2020-09-18
wytzsjzly 2020-08-17
88407710 2020-08-17
ChinaJoeEE 2020-08-16
CyborgLin 2020-08-15
Blueberry 2020-08-15
PinkBean 2020-08-11
katanaFlower 2020-08-03
hunningtu 2020-07-30
阿债的方寸天地 2020-06-28
pingyan 2020-06-25
wytzsjzly 2020-06-25
阳光岛主 2020-06-25
阿债的方寸天地 2020-06-16