Php 设置站点密码保护后curl失败

Php 设置站点密码保护后curl失败,php,php-curl,Php,Php Curl,最近,我们使登台站点受到密码保护。这意味着,如果我想访问mysitestaging.com,浏览器会询问user/pass。请注意,它不是站点登录。浏览器请求用户/通行证 之后,我的PHP代码的一部分返回错误。以下是cURl代码: $prefix = GetProtocol().GetDomain(); $url = $prefix."/ecomm-merged/ajax.php?action=dosomething"; $url .= "&sid=".$sid;

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

之后,我的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>

401未经授权
正文{边距:20px;字体系列:helvetica,无衬线;最大宽度:800px;}
.错误{颜色:#e00;}
前{字体大小:16px;}
h1{字体大小:28px;}
未经授权
此服务器无法验证您是否
您有权访问该文档
请求。要么你提供的是错误的
凭据(例如,错误的密码)或您的
浏览器不了解如何提供
所需的凭据


如果我在输入user/pass-everything后在浏览器中键入要卷曲到的URL,则会起作用。那么我该如何解决这个问题并为cURL添加user/pass功能呢

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

...
$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);