cURL call with a port in the url/location not work

2019-07-08 02:34发布

问题:

I am trying to make a cURL call to a url that looks like this:

https://example.com:9000/test

When I execute the following code, I get curl error 7 couldn't connect to host.

$headers = array(
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_CONNECTTIMEOUT => 5,
  CURLOPT_TIMEOUT => 10,
  CURLOPT_URL => 'https://example.com:9000/test',
);
$headers[CURLOPT_SSL_VERIFYPEER] = FALSE;
$headers[CURLOPT_SSL_VERIFYHOST] = 2;
$ch = curl_init(); 
curl_setopt_array($ch, $headers);
$response = curl_exec($ch);

If I set the url to https://example.com/test, I am able to connect to the host, just not to what I need to get.

I have also tried setting <code>CURLOPT_PORT => 9000</code> with the same result (error 7).

One other note, I am able to use cURL with the url on some machines but not others. My Windows machine works fine, but the linux server I'm on is the one having issues. Another linux server seems to work fine as well.

EDIT: Server is shared hosting on hostgator.com.

回答1:

If anyone else has this problem, contact your server host. I contacted hostgator.com and they responded with the following:

I have opened the outbound port 9000 for you.

Everything works now.



回答2:

check the url in IE browser. if it works fine then remove the timeout params and try.



标签: php curl libcurl