Apache 上的 cURL 显示 ..“访问被禁止!错误403”
我正在使用 cURL 测试简单的 API。它是从一个 Apache 服务器(的 php 文件)调用另一台 Apache 服务器(的 php 文件)。本地测试就可以了。但是当我用我的网络电脑进行测试时,它显示以下 403 错误:
访问被禁止! 您无权访问所请求的对象。它要么是读保护的,要么是服务器无法读取的。 如果您认为这是服务器错误,请联系网站管理员。 调用方服务器(服务器 1)的错误 403
代码为:
function apicall($request_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
$request_url = 'http://192.168.1.205/api.php?cname=David';
$response = apicall($request_url);
应答服务器(服务器 2)的代码为:
echo "Hello ".$_GET['cname'];
在两个 Apache 上都启用了 cURL。那么为什么呢?我需要做什么?
I am testing simple API with cURL. It is, calling from (a php file of) one Apache Server to (a php file of) another Apache Server. It is ok testing locally. But when i test with my network PCs, it showing following 403 error:
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
Codes for Caller Server (Server 1) are:
function apicall($request_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
$request_url = 'http://192.168.1.205/api.php?cname=David';
$response = apicall($request_url);
Codes for Answering Server (Server 2) are:
echo "Hello ".$_GET['cname'];
cURL is enabled on both Apache. So why? What do i need to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与 cURL 无关,这是您的 Apache 配置的问题。
Apache 的配置方式是,
api.php
中的资源对于运行脚本的计算机不可用。在 Apache 配置中,对于根目录,您需要检查这些指令:
看看 此及其下面的部分。
或者,您可能在 api.php 中的某个位置有一些代码,看起来像这样:
...但是,根据您在代码中所说的内容,我认为这与 Apache 配置有关。
This is nothing to do with cURL, it is your Apache configuration that is the problem.
Apache is configured in such a way the the resource at
api.php
is not available to the machine on which your script is running.In you Apache configuration, for the root directory, you need to inspect these directives:
Have a look at this and the sections immediately below it.
Alternatively, you may have some code somewhere in
api.php
that looks something like this:...however, based on what you say is in your code, I think this is about the Apache configuration.
如果您使用的是 WAMP,请确保您“上线”服务器。
其次,
你的 htaccess 是否阻止了它?
If you are using WAMP, make sure you "put online" the server.
Secondly,
Is you htaccess blocking it ?