php cURL 到本地主机 returns 开放端口上的权限被拒绝
php cURL to localhost returns permission denied on an open port
当我尝试使用 php cURL 库向端口 4321 上的本地主机发出 cURL 请求时,我遇到了权限被拒绝的错误。这对于 [=45= 的人来说可能真的很容易或显而易见] 进入这个之前。
我能够从局域网上的另一个系统向生产服务器发出相同的 cURL 请求。例如,如果在局域网上的另一个系统上,我使用下面的函数发出请求,其中 $host='http://192.168.1.100:4321'
然后一切都像它应该的那样工作。如果我在系统本身 $host='http://localhost:4321'
或 $host='http://127.0.0.1:4321'
或 $host='::1:4321'
上 运行 然后我得到一个 cURL 错误 "Permission Denied"
我为我非常简单的请求写的函数是:
function makeRequest($host,$data){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $host);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = json_decode(curl_exec($ch),true);
if(!empty(curl_error($ch))){
$result = print_r(curl_error($ch).' - '.$host);
}
curl_close($ch);
return $result;
}
系统是centos 7服务器。 运行 firewall-cmd --list-all
显示我打开的端口
ports: 443/tcp 80/tcp 4321/tcp
如果您有任何想法,或者需要我检查设置,请随时询问。
编辑
主机文件看起来像
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
EDIT2
当我对同一个端口使用命令行 curl 时,一切都会恢复正常。
/]$ curl -v localhost:4321
* About to connect() to localhost port 4321 (#0)
* Trying ::1...
* Connected to localhost (::1) port 4321 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:4321
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Length: 774....
我在以下位置找到了问题的答案:
Getting permission denied while Posting xml using Curl?
SELinux 的问题,解决方法是运行:
sudo setsebool httpd_can_network_connect 1
我可以使用 php cURL 库访问世界上所有其他网站,但不能访问不同端口上的本地主机,而我能够访问本地主机,这对我来说没有意义来自命令行 cURL。
当我尝试使用 php cURL 库向端口 4321 上的本地主机发出 cURL 请求时,我遇到了权限被拒绝的错误。这对于 [=45= 的人来说可能真的很容易或显而易见] 进入这个之前。
我能够从局域网上的另一个系统向生产服务器发出相同的 cURL 请求。例如,如果在局域网上的另一个系统上,我使用下面的函数发出请求,其中 $host='http://192.168.1.100:4321'
然后一切都像它应该的那样工作。如果我在系统本身 $host='http://localhost:4321'
或 $host='http://127.0.0.1:4321'
或 $host='::1:4321'
上 运行 然后我得到一个 cURL 错误 "Permission Denied"
我为我非常简单的请求写的函数是:
function makeRequest($host,$data){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $host);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = json_decode(curl_exec($ch),true);
if(!empty(curl_error($ch))){
$result = print_r(curl_error($ch).' - '.$host);
}
curl_close($ch);
return $result;
}
系统是centos 7服务器。 运行 firewall-cmd --list-all
显示我打开的端口
ports: 443/tcp 80/tcp 4321/tcp
如果您有任何想法,或者需要我检查设置,请随时询问。
编辑 主机文件看起来像
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
EDIT2
当我对同一个端口使用命令行 curl 时,一切都会恢复正常。
/]$ curl -v localhost:4321
* About to connect() to localhost port 4321 (#0)
* Trying ::1...
* Connected to localhost (::1) port 4321 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:4321
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Length: 774....
我在以下位置找到了问题的答案: Getting permission denied while Posting xml using Curl?
SELinux 的问题,解决方法是运行:
sudo setsebool httpd_can_network_connect 1
我可以使用 php cURL 库访问世界上所有其他网站,但不能访问不同端口上的本地主机,而我能够访问本地主机,这对我来说没有意义来自命令行 cURL。