Why Automate Routine with PHP? Or How to Make the Computer Work for You
\n\nImagine: every morning you log into the server, run a database backup, check error logs, generate yesterday's report, and send it to your boss. And then — again, and again. Boring? You bet. And more importantly, it's foolish to waste a programmer's time on what a machine can do.
\n\nPHP is not just about websites. This language is great for task automation in PHP: from simple file renaming to complex website parsing. In this article, I'll show you 10 real examples that will save you hours of work. All scripts are working and tested — take them and use them.
\n\nBy the way, if you're looking for ready-made solutions for automation with PHP — you've come to the right place. Let's go!
\n\n1. Automatic Email Sending with Attachment (PDF Report)
\n\nThe most common request from business: "Make reports come by themselves." Below is a simple script that sends an email with a PDF file.
\n\n<?php\nfunction sendReportWithAttachment($to, $subject, $body, $filePath) {\n $boundary = md5(time());\n \n $headers = "MIME-Version: 1.0\\r\\n";\n $headers .= "From: report@mysite.com\\r\\n";\n $headers .= "Content-Type: multipart/mixed; boundary=\\"{$boundary}\\"\\r\\n";\n \n $message = "--{$boundary}\\r\\n";\n $message .= "Content-Type: text/plain; charset=utf-8\\r\\n\\r\\n";\n $message .= $body . "\\r\\n\\r\\n";\n \n if (file_exists($filePath)) {\n $content = file_get_contents($filePath);\n $content = chunk_split(base64_encode($content));\n $message .= "--{$boundary}\\r\\n";\n $message .= "Content-Type: application/pdf; name=\\"" . basename($filePath) . "\\"\\r\\n";\n $message .= "Content-Disposition: attachment; filename=\\"" . basename($filePath) . "\\"\\r\\n";\n $message .= "Content-Transfer-Encoding: base64\\r\\n\\r\\n";\n $message .= $content . "\\r\\n";\n }\n \n $message .= "--{$boundary}--";\n \n return mail($to, $subject, $message, $headers);\n}\n\n// Usage:\nsendReportWithAttachment(\n 'boss@company.com',\n 'Daily report for ' . date('d.m.Y'),\n 'PDF report attached. Details in the file.',\n '/var/reports/daily_report.pdf'\n);\n?>\n\nHow to automate: put this script in cron to run once a day — and reports will be sent automatically.
\n\n2. Website Parsing and Saving Data to CSV
\n\nLet's say you need to collect competitor prices or a list of vacancies. Parsing in PHP is easy. Use cURL and DOMDocument.
\n\n<?php\nfunction parseSiteToCSV($url, $cssSelector, $outputFile) {\n $ch = curl_init();\n curl_setopt($ch, CURLOPT_URL, $url);\n curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);\n curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)');\n \n $html = curl_exec($ch);\n curl_close($ch);\n \n if (!$html) {\n return false;\n }\n \n $dom = new DOMDocument();\n @$dom->loadHTML($html);\n $xpath = new DOMXPath($dom);\n \n $items = $xpath->query($cssSelector);\n \n $file = fopen($outputFile, 'w');\n fputcsv($file, ['Text', 'Link']);\n \n foreach ($items as $item) {\n $text = trim($item->textContent);\n $link = $item->getAttribute('href') ?: '—';\n fputcsv($file, [$text, $link]);\n }\n \n fclose($file);\n return true;\n}\n\n// Example: parse news headlines from a site\nparseSiteToCSV(\n 'https://news.ycombinator.com/',\n '//tr[@class="athing"]/td[@class="title"]/a',\n 'hacker_news_titles.csv'\n);\n?>\n\nThis example of task automation in PHP can be run once an hour — and you'll get fresh data without a single click.
\n\n