保护网站密码后卷曲失败

时间:2017-08-30 03:12:50

标签: php php-curl

最近我们使登台网站的密码受到保护。这意味着如果我想去mysitestaging.com,浏览器会要求用户/通行证。请注意,它不是站点登录。浏览器要求用户/传递

之后,我的部分PHP代码返回错误。这是cURl代码:

$prefix = GetProtocol().GetDomain();
    $url = $prefix."/ecomm-merged/ajax.php?action=dosomething";

    $url .= "&sid=".$sid;
    $url .= "&sessionid=".$sessionid;

    doLog("Ajax URL: " . $url);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, ‘curl’);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);

    $result = curl_exec($ch);

以下是我在日志中看到的内容:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
<style>
    body {margin: 20px; font-family: helvetica, sans-serif; max-width: 800px;}
    .error {color: #e00;}
    pre {font-size: 16px;}
    h1 {font-size: 28px;}
</style>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

如果我输入用户/传递一切正常后,我在浏览器中键入了我正在浏览的URL。那么我该如何解决这个问题并将用户/传递功能添加到cURL

1 个答案:

答案 0 :(得分:1)

由于您的网站受密码保护,您需要在执行cURL时传递用户名和密码,使用CURLOPT_USERPWD选项,例如:

...
$your_username = "your-basic-auth-username";
$your_password = "your-basic-auth-password";
$ch = curl_init();
...
curl_setopt($ch, CURLOPT_USERPWD, $your_username . ":" . $your_password);
...
$result = curl_exec($ch);