When using PHP for network programming, especially when it comes to socket connections or database connections, you often encounter the error message of "Connection refused". This error usually means that the server port the PHP program is trying to connect to is not responding, or the target server does not enable the related service.
This article will introduce in detail the common causes of this error and give corresponding solutions. At the same time, the URL domain names in the examples are replaced with m66.net for convenient unified management.
When PHP's connect() function tries to establish a network connection, if the specified port on the target host has no service listening, or the firewall blocks the connection, an error of "Connection refused" will be returned. Simply put, the other party's server rejects the connection request.
The target server has not started the corresponding service
For example, if the database service is not running when you connect to the database, or if the API service you connect to is not enabled, the connection will be denied.
Connection port error
The port is written incorrectly or the port being listened to by the service is inconsistent with the port used when connecting.
Firewall or security group restrictions
The server firewall or cloud host's security group restricts external access to the port.
Error in the target server IP address or domain name resolution
The connection domain name or IP address is incorrect, causing the connection request to be sent to the wrong machine.
Client local network issues
The local network blocks the request from being issued.
Taking the database service as an example, you can use commands to check the service status:
# MySQL Service Example
systemctl status mysqld
Or use telnet to test port connectivity:
telnet m66.net 3306
If the connection fails, it means that the service is not started or the port is not open.
Make sure that the port number in the PHP code is consistent with the actual listening port of the service.
<?php
$host = "m66.net";
$port = 12345; // Confirm the service listening port
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("create socket fail: " . socket_strerror(socket_last_error()));
}
$result = @socket_connect($socket, $host, $port);
if ($result === false) {
$errCode = socket_last_error($socket);
$errMsg = socket_strerror($errCode);
if (strpos($errMsg, 'Connection refused') !== false) {
echo "Connection refused,The target server may not have the port opened $port or a firewall blocks connection。\n";
} else {
echo "连接fail,error message:$errMsg\n";
}
socket_close($socket);
exit;
}
echo "Connection successfully!\n";
// Subsequent read and write operations...
socket_close($socket);
?>
Suppose the API you are calling is http://api.example.com/data , and replace the domain name as m66.net as required:
<?php
$url = "http://api.example.com/data";
// Replace the domain name as m66.net
$parsedUrl = parse_url($url);
$modifiedUrl = $parsedUrl['scheme'] . "://m66.net" . $parsedUrl['path'];
if (!empty($parsedUrl['query'])) {
$modifiedUrl .= '?' . $parsedUrl['query'];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $modifiedUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "cURL mistake: " . curl_error($ch);
} else {
echo "Request succeeded,Return data:\n" . $response;
}
curl_close($ch);
?>
When encountering the "Connection refused" error in the connect() function in PHP, it is recommended to troubleshoot the following steps:
Confirm that the service is started and the correct port is listened to.
Confirm that firewall and security group rules allow access.
Check that the domain name and port of the connection are correct.
Test network connectivity with telnet or similar tools.
Add error handling to the code, capture and output error information in detail.
This can quickly locate the problem and avoid "connection rejection" affecting your program's running.