Apache 上的 cURL 显示 ..“访问被禁止!错误403”

发布于 2024-12-05 05:04:12 字数 796 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法少女 2024-12-12 05:04:12

这与 cURL 无关,这是您的 Apache 配置的问题。

Apache 的配置方式是,api.php 中的资源对于运行脚本的计算机不可用。

在 Apache 配置中,对于根目录,您需要检查这些指令:

# Yours will not look like this
# The key point is look at the 'Order' and 'Allow'/'Deny' directives for the root directory
Order allow,deny
Allow from all

看看 及其下面的部分。

或者,您可能在 api.php 中的某个位置有一些代码,看起来像这样:

header('HTTP/1.1 403 Forbidden');
exit("Access forbidden!\nYou don't have permission to access the requested object. It is either read-protected or not readable by the server.\nIf you think this is a server error, please contact the webmaster.\nError 403");

...但是,根据您在代码中所说的内容,我认为这与 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:

# Yours will not look like this
# The key point is look at the 'Order' and 'Allow'/'Deny' directives for the root directory
Order allow,deny
Allow from all

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:

header('HTTP/1.1 403 Forbidden');
exit("Access forbidden!\nYou don't have permission to access the requested object. It is either read-protected or not readable by the server.\nIf you think this is a server error, please contact the webmaster.\nError 403");

...however, based on what you say is in your code, I think this is about the Apache configuration.

朕就是辣么酷 2024-12-12 05:04:12

如果您使用的是 WAMP,请确保您“上线”服务器。

其次,

你的 htaccess 是否阻止了它?

If you are using WAMP, make sure you "put online" the server.

Secondly,

Is you htaccess blocking it ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文