带有端口的 IP URL 在 cURL PHP 中不起作用
$url = 'http://IP:PORT/API_URL';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error: ' . curl_error($curl);
}
curl_close($curl);
echo $curlData;
上面的代码是使用最新的 PHP 返回Error: Failed to connect to [IP] port [PORT]: Connection refused
,并使用旧 PHP 返回超时。
第二种情况:
$url = 'http://IP/API_URL';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_PORT, [PORT]);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
echo 'curl_getinfo: ';
echo "<pre>";
print_r(curl_getinfo($curl));
echo "</pre>";
if (curl_errno($curl)) {
echo 'Error: ' . curl_error($curl);
}
curl_close($curl);
echo $curlData;
而上述案例的输出是:
curl_getinfo:
Array
(
[url] => http://IP:PORT/API_URL
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.201014
[namelookup_time] => 2.7E-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] =>
[certinfo] => Array
(
)
[primary_port] => 0
[local_ip] =>
[local_port] => 0
)
Error: Failed to connect to [IP] port [PORT]: Connection refused
我会测试端口是否打开。您可以使用telnet IP PORT
.端口很可能被阻止。另一种可能性是您正在尝试连接到强制SSL的端口。您在代码中显然已停用。请尝试生成有效的证书,然后重试。