How can the output of fsockopen be correctly written into the database instead of "Resource id #"?

When using fsockopen to open a network connection in PHP, the output is a resource identifier, not the actual data received from the connection. To correctly write the output into a database, you need to read the data from the connection using functions like fread or fgets. Once you have the data, you can then insert it into the database.

// Open a network connection
$fp = fsockopen("example.com", 80, $errno, $errstr, 30);

// Check if the connection was successful
if (!$fp) {
    echo "Error: $errstr ($errno)";
} else {
    // Send a request
    fwrite($fp, "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n");

    // Read the response
    $response = '';
    while (!feof($fp)) {
        $response .= fgets($fp, 1024);
    }

    // Close the connection
    fclose($fp);

    // Insert the response into the database
    $db = new mysqli("localhost", "username", "password", "database");
    $stmt = $db->prepare("INSERT INTO table_name (response) VALUES (?)");
    $stmt->bind_param("s", $response);
    $stmt->execute();
    $stmt->close();
    $db->close();
}