我使用的是带有 LAMP 的 Linux Fedora 20。 我使用这个简单的 php 脚本,它使用curl:
// test.php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_URL, "http://xx.xx.xx.xx:8080/file.xml");
curl_setopt($ch, CURLOPT_USERPWD, FEDORA_USER.":".FEDORA_PASS);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
在 php cli 中,我制作:
php test.php
Curl works.
在 php cli 中,我制作:
curl http://xx.xx.xx.xx:8080/file.xml
Curl works.
但是在导航器(chrome、firefox...)中,curl 不起作用。错误是:
Fatal error: (7) `curl_exec(...)` returned false.
Error: Failed to connect to xx.xx.xx.xx: Permission denied.
Info: Array ( [url] => http://xx.xx.xx.xx:8080/file.xml
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 1.6E-5
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 172.17.100.8
[certinfo] => Array ( )
[primary_port] => 8080
[local_ip] =>
[local_port] => 0
)
in /home/...../test.php on line 139
导航器使用/etc/php.ini
Php-cli 使用/etc/php.ini
我不明白为什么它在导航器上不起作用,问题不在服务器端。你能帮我吗?
最佳答案
如果您发现问题出在 SELinux 上,您可以使用 setsebool -P httpd_can_network_connect on
允许 httpd 访问网络,并避免禁用所有 SELinux。请参阅https://stackoverflow.com/a/50808173/404960
关于php - Curl 在 php-cli 和 cli 中工作,但在 php 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31681633/