PHP Network
Work with network protocols and connections
🌐 What is PHP Network?
PHP Network functions allow you to communicate with remote servers, fetch web content, send emails, and work with various network protocols like HTTP, FTP, and sockets.
<?php
// Fetch content from a URL
$content = file_get_contents("https://api.example.com/data");
echo "Data fetched successfully!";
?>
Key Network Functions
HTTP Requests
Fetch data from URLs
file_get_contents($url);
Sockets
Low-level network connections
fsockopen($host, $port);
Send emails via SMTP
mail($to, $subject, $message);
DNS Lookup
Resolve domain names
gethostbyname($hostname);
🔹 Fetching URL Content
Use file_get_contents() to retrieve data from remote URLs. This is the simplest way to make HTTP GET requests and fetch web content or API responses.
<?php
// Fetch content from a URL
$url = "https://jsonplaceholder.typicode.com/posts/1";
$response = file_get_contents($url);
if ($response !== FALSE) {
$data = json_decode($response, true);
echo "Title: " . $data['title'];
} else {
echo "Failed to fetch data";
}
?>
Output:
Title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
🔹 Using cURL for Advanced Requests
cURL provides more control over HTTP requests with support for headers, POST data, authentication, and various protocols. It's ideal for complex API interactions.
<?php
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Execute request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL session
curl_close($ch);
echo "HTTP Code: " . $httpCode;
?>
Output:
HTTP Code: 200
🔹 POST Request with cURL
Send data to remote servers using POST requests. This is commonly used for submitting forms, uploading data, or interacting with RESTful APIs.
<?php
$ch = curl_init();
$data = array('name' => 'John', 'email' => '[email protected]');
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/users");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
echo "User created successfully";
?>
Output:
User created successfully
🔹 Socket Connection
Create low-level network connections using sockets. Sockets allow direct communication with servers using TCP/IP protocols for custom network applications.
<?php
// Open socket connection
$socket = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$socket) {
echo "Error: $errstr ($errno)";
} else {
// Send HTTP request
fwrite($socket, "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n");
// Read response (first line)
$response = fgets($socket);
echo "Response: " . $response;
fclose($socket);
}
?>
Output:
Response: HTTP/1.1 200 OK
🔹 DNS Lookup
Resolve domain names to IP addresses using DNS functions. This helps you find the server address behind a domain name or verify DNS configuration.
<?php
// Get IP address from hostname
$hostname = "www.google.com";
$ip = gethostbyname($hostname);
echo "Hostname: " . $hostname . "<br>";
echo "IP Address: " . $ip;
// Get hostname from IP
$host = gethostbyaddr($ip);
echo "<br>Reverse lookup: " . $host;
?>
Output:
Hostname: www.google.com
IP Address: 142.250.185.36
Reverse lookup: lga25s78-in-f4.1e100.net
🔹 Checking URL Headers
Retrieve HTTP headers from a URL without downloading the entire content. This is useful for checking if a resource exists or getting metadata.
<?php
$url = "https://www.example.com";
$headers = get_headers($url, 1);
echo "Status: " . $headers[0] . "<br>";
echo "Content-Type: " . $headers['Content-Type'] . "<br>";
echo "Server: " . $headers['Server'];
?>
Output:
Status: HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: Apache