无法使用端口号发出 php curl 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7188988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:13:22  来源:igfitidea点击:

Unable to make php curl request with port number

phpcurlport

提问by druveen

I am unable to make a php curl request with port number , without port number i am getting response properly.

我无法使用端口号发出 php curl 请求,如果没有端口号,我将得到正确的响应。

Through terminal when i do curl http://www.sample.com:8088i am getting response back properly. But not through php curl on doing curl_inf() i am getting

通过终端,当我做 curl http://www.sample.com:8088时,我得到了正确的回复。但不是通过 php curl 做 curl_inf() 我得到

Array ( [url] => http://www.sample.com:8088/ [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => 0 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [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 [certinfo] => Array ( ) ) 

My code

我的代码

$url = "http://www.sample.com:8088/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$report=curl_getinfo($ch);
print_r($report);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

this code give me response from ubuntu , but not giving response from cent os if port number is specified.

这段代码给了我来自 ubuntu 的响应,但如果指定了端口号,则不会给出来自 cent os 的响应。

Please let me know the how to fix it.

请让我知道如何修复它。

THanks in advance.

提前致谢。

回答by Mário Rodrigues

Try to set the port like this:

尝试像这样设置端口:

curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);

or:

或者:

curl_setopt($ch, CURLOPT_PORT, 8088);

回答by Andrej Ludinovskov

Try to set

尝试设置

curl_setopt($ch,CURLOPT_PORT, 8088);

See more info http://php.net/manual/en/function.curl-setopt.php

查看更多信息http://php.net/manual/en/function.curl-setopt.php

回答by J0HN

Add

添加

curl_setopt($ch, CURLOPT_PORT, 8088);

Curl_setoptreference

curl_setopt参考

回答by Kannan Prasad

Make sure that the port 8080 is enabled in Cent OS

确保在 Cent OS 中启用了端口 8080

and try this curl_setopt($ch, CURLOPT_PORT, 8088);

试试这个 curl_setopt($ch, CURLOPT_PORT, 8088);

回答by Giorgi

"setsebool httpd_can_network_connect on" resolves the problem for me

“setsebool httpd_can_network_connect on”为我解决了问题

回答by druveen

I went to server admin with this problem and he changed configuration in centos

我带着这个问题去了服务器管理员,他在centos中更改了配置

in folder /etc/sysconfig/

在文件夹 /etc/sysconfig/

he edited file selinux and change SELINUX=disabled and restated it.

他编辑了文件 selinux 并更改 SELINUX=disabled 并重新声明它。

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled

and my problem got solved.

我的问题得到了解决。