使站点密码受保护后 curl 失败
curl failing after making site password protected
最近我们对暂存站点进行了密码保护。这意味着如果我想去 mysitestaging.com,浏览器会要求 user/pass。请注意,它不是站点登录。浏览器要求 user/pass
在那之后我的 PHP 代码 returns 的一部分错误。这是卷曲代码:
$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>
如果我在输入 user/pass 后在浏览器中输入我正在 URL 的 URL 一切正常。那么我该如何解决这个问题并将 user/pass 功能添加到 cURL
由于您的网站受密码保护,因此在执行 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);
最近我们对暂存站点进行了密码保护。这意味着如果我想去 mysitestaging.com,浏览器会要求 user/pass。请注意,它不是站点登录。浏览器要求 user/pass
在那之后我的 PHP 代码 returns 的一部分错误。这是卷曲代码:
$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>
如果我在输入 user/pass 后在浏览器中输入我正在 URL 的 URL 一切正常。那么我该如何解决这个问题并将 user/pass 功能添加到 cURL
由于您的网站受密码保护,因此在执行 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);