PHP में रूटीन को ऑटोमेट क्यों करें? या कंप्यूटर को अपने लिए काम करवाएं
\n\nसोचिए: हर सुबह आप सर्वर पर लॉग इन करते हैं, डेटाबेस का बैकअप लेते हैं, एरर लॉग चेक करते हैं, कल की रिपोर्ट जनरेट करते हैं और बॉस को भेजते हैं। और फिर—बार-बार। उबाऊ? बिल्कुल। और सबसे बड़ी बात—एक प्रोग्रामर का समय ऐसे कामों में बर्बाद करना बेवकूफी है जो मशीन कर सकती है।
\n\nPHP सिर्फ वेबसाइटों के लिए नहीं है। यह भाषा PHP में कार्य स्वचालन के लिए बहुत उपयुक्त है: फाइलों का नाम बदलने से लेकर जटिल वेब स्क्रैपिंग तक। इस लेख में मैं 10 वास्तविक उदाहरण दिखाऊंगा जो आपके घंटों का काम बचाएंगे। सभी स्क्रिप्ट काम करने वाली और परीक्षित हैं—बस लें और उपयोग करें।
\n\nवैसे, अगर आप PHP ऑटोमेशन के लिए तैयार समाधान ढूंढ रहे हैं—तो आप सही जगह पर हैं। चलिए शुरू करते हैं!
\n\n1. अटैचमेंट के साथ ऑटोमैटिक ईमेल भेजना (PDF रिपोर्ट)
\n\nव्यवसायों की सबसे आम मांग: "रिपोर्ट खुद-ब-खुद आनी चाहिए।" नीचे एक सरल स्क्रिप्ट है जो PDF फाइल के साथ ईमेल भेजती है।
\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// उपयोग:\nsendReportWithAttachment(\n 'boss@company.com',\n 'दैनिक रिपोर्ट ' . date('d.m.Y'),\n 'अटैचमेंट में PDF रिपोर्ट है। विवरण फाइल में।',\n '/var/reports/daily_report.pdf'\n);\n?>\n\nकैसे ऑटोमेट करें: इस स्क्रिप्ट को cron में दिन में एक बार चलाने के लिए सेट करें—और रिपोर्ट अपने आप भेजी जाएगी।
\n\n2. वेब स्क्रैपिंग और CSV में डेटा सेव करना
\n\nमान लीजिए, आपको प्रतिस्पर्धियों की कीमतें या नौकरी की सूची इकट्ठा करनी है। PHP में वेब स्क्रैपिंग बहुत आसान है। cURL और 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, ['टेक्स्ट', 'लिंक']);\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// उदाहरण: वेबसाइट से समाचार शीर्षक स्क्रैप करना\nparseSiteToCSV(\n 'https://news.ycombinator.com/',\n '//tr[@class="athing"]/td[@class="title"]/a',\n 'hacker_news_titles.csv'\n);\n?>\n\nयह PHP कार्य स्वचालन का उदाहरण हर घंटे चलाया जा सकता है—और बिना एक क्लिक के ताजा डेटा प्राप्त करें।
\n\n