我在服务器上部署了一个API,现在使用基本身份验证进行保护。
我还有一个在运行APi的SAME服务器上运行的脚本,需要调用API。
脚本:test.php
<?php
$url = 'https://my_site.myapi/account/add/{"account_id":"1234555"}
$username = 'myname';
$password = 'mypassword';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
当我尝试从浏览器调用test.php时:
https://my_site.myapi/test.php
我被提升为用户名和密码。我没有收到错误消息。有什么想法吗?
DocumentRoot /var/www/mysite/web/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/api_section_1">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /usr/local/apache/passwd/passwords
</Directory>
Alias /winapi /var/www/another_section/
<Directory "/var/www/another_section">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /usr/local/apache/passwd/passwords
</Directory>
答案 0 :(得分:1)
如果系统提示您输入密码,则不是来自您的密码,而是来自apache。我的猜测是你的https://my_site.myapi/test.php页面也被基本身份验证错误地保护了。
curl_exec()将返回一个错误,指示需要进行身份验证,而不是提示您进行身份验证。
答案 1 :(得分:0)
您的基本身份验证是否需要任何编码 - 例如,用户名和密码令牌可能需要进行base64编码。
要检查的另一件事是细节会进入Authorization标头 - 检查请求标头以查看是否是这种情况。