PHP Network functions help you talk about hosts, responses, and addresses in plain PHP. You can ask DNS whether a name has an A or MX record, turn a host name into an IP address, send HTTP headers and cookies before any page output, convert IPv4 and IPv6 addresses, open a TCP socket, or write a line to the system log.
Neighboring tutorials that often sit next to this work: SimpleXML for parsing API XML, Variables for types and casting, Filesystem for local files beside network I/O, and JSON for API payloads.
Code
PHP Examples
Three short scripts that show the core Network ideas. The first uses local host helpers so the sample stays easy to follow; the second and third are fully self-contained. Each block includes a fixed View Output panel that matches this tutorial layout.
Example 1 — DNS lookup helpers
Resolve localhost, reverse-look up the loopback address, and show how checkdnsrr() answers a yes/no DNS question.
PHP
<?php
echo 'localhost IP: ', gethostbyname('localhost'), PHP_EOL;
$ips = gethostbynamel('localhost');
echo 'localhost list: ', $ips ? implode(', ', $ips) : 'none', PHP_EOL;
echo 'reverse 127.0.0.1: ', gethostbyaddr('127.0.0.1'), PHP_EOL;
// Illustrative public name: result is true/false from DNS
echo 'example.com has A?: ', checkdnsrr('example.com', 'A') ? 'yes' : 'no', PHP_EOL;
echo 'this host: ', gethostname(), PHP_EOL;
?>
http_response_code(201) sets the response status. Calling it again with no argument prints the current code, 201.
header() queues plain header lines. They leave with the response as long as no body was printed yet.
setcookie() adds a Set-Cookie header. The options array sets path and HttpOnly in modern PHP.
headers_sent() is still no here because the echo lines come after the header calls in this script—until the first byte of body is flushed, you can still add headers.
headers_list() returns the headers PHP has prepared. Cookie lines appear as Set-Cookie: ... in that list.
Example 3 — IP address conversion
Convert an IPv4 address to a number and back, then pack and unpack the same address with inet_pton() / inet_ntop().
They are the built-in PHP functions for everyday network work: DNS and host lookups, protocol and service name tables, HTTP response headers and cookies, IP address conversion, TCP sockets with fsockopen(), writing to syslog, and (in PHP 8.4) reading the last HTTP response headers and parsing a request body with request_parse_body().
Start with the DNS helpers gethostbyname() and checkdnsrr() so you understand host names versus IP addresses. Then learn header() and setcookie() for web responses, and ip2long() with long2ip() for IPv4 conversion. Open sockets and syslog after those basics feel clear.
None in practice. dns_check_record() is an alias of checkdnsrr(). Both ask the DNS for a record type (A, MX, and so on) and return true or false. Prefer the shorter name checkdnsrr() in new code, and treat the alias page as the same idea.
HTTP headers must leave PHP before any body output. If you echo, print HTML, or have a blank line before <?php, headers_sent() becomes true and later header() or setcookie() calls fail. Call them first, or buffer output with ob_start() when you need to decide headers later.
PHP 8.4 adds http_get_last_response_headers() and http_clear_last_response_headers() to inspect headers from the last HTTP wrapper request, and request_parse_body() to parse multipart or URL-encoded request bodies into $_POST and $_FILES style data. Check your PHP version before you rely on them.
Use fsockopen() or pfsockopen() when you need a raw TCP (or SSL) stream and want to speak a simple protocol yourself. For everyday HTTP APIs, prefer stream wrappers, cURL, or an HTTP client library. The socket_* network aliases are older names for stream helpers such as stream_get_meta_data().