Unable to make php curl request with port number

2020-02-01 18:53发布

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

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

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.

Please let me know the how to fix it.

THanks in advance.

标签: php curl port
6条回答
在下西门庆
2楼-- · 2020-02-01 19:00

Add

curl_setopt($ch, CURLOPT_PORT, 8088);

Curl_setopt reference

查看更多
Viruses.
3楼-- · 2020-02-01 19:03

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

in folder /etc/sysconfig/

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

# 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.

查看更多
够拽才男人
4楼-- · 2020-02-01 19:05

"setsebool httpd_can_network_connect on" resolves the problem for me

查看更多
我命由我不由天
5楼-- · 2020-02-01 19:10

Try to set the port like this:

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

or:

curl_setopt($ch, CURLOPT_PORT, 8088);
查看更多
来,给爷笑一个
6楼-- · 2020-02-01 19:21

Make sure that the port 8080 is enabled in Cent OS

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

查看更多
Bombasti
7楼-- · 2020-02-01 19:23

Try to set

curl_setopt($ch,CURLOPT_PORT, 8088);

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

查看更多
登录 后发表回答