求PHP实现:从网站提取指定字符串后4字符并保存为plid.js
Got it, let's break down how to replicate your VB.NET functionality in PHP and set up a cron job for it—super straightforward once you know the steps!
Solution Overview
We'll split this into two key parts: writing a PHP script that mirrors your existing logic, then configuring a cron job to run it automatically on your server.
1. PHP Script Implementation
This script will handle fetching the target webpage, locating your specified string, extracting the 4 characters right after it, and saving the result to plid.js.
Here's a clean, functional version (customize the placeholders to match your needs):
<?php // Replace these with your actual target URL and search string $targetUrl = "https://your-target-site.com"; $searchString = "your-specified-search-text"; // Fetch webpage content (use cURL if file_get_contents is disabled) $pageContent = file_get_contents($targetUrl); if ($pageContent === false) { error_log("Failed to pull content from $targetUrl"); exit(1); } // Find the position of your search string $stringPos = strpos($pageContent, $searchString); if ($stringPos === false) { error_log("Search string '$searchString' not found on the page"); $result = ""; // Set a default value if needed } else { // Calculate start position for the 4 characters post-search-string $startPos = $stringPos + strlen($searchString); $result = substr($pageContent, $startPos, 4); } // Save result to plid.js (use absolute path for cron reliability) $savePath = "/var/www/html/plid.js"; // Update to your server's actual path if (file_put_contents($savePath, $result) === false) { error_log("Couldn't write result to $savePath"); exit(1); } exit(0); ?>
Quick Notes for the Script:
- If your server blocks
file_get_contents(common in restrictive environments), swap it for this cURL alternative:$ch = curl_init($targetUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects if needed $pageContent = curl_exec($ch); curl_close($ch); - Always use an absolute path for
$savePathwhen running via cron—relative paths can break because cron uses a different working directory. - Make sure the directory where
plid.jsis saved has write permissions for the user running the cron job (usually your server account or web server user likewww-data).
2. Set Up the Cron Job
Now let's schedule the script to run automatically:
Open your crontab:
Run this command in your server's terminal:crontab -eThis will open your cron config in a text editor (nano is common).
Add the cron rule:
Add a line like this to define when the script runs. For example, to run it every day at 2 AM:0 2 * * * /usr/bin/php /path/to/your/script.phpLet's break down the cron syntax:
0: Minute (0 = top of the hour)2: Hour (2 = 2 AM, use 24-hour format)*: Day of month (any day)*: Month (any month)*: Day of week (any day)/usr/bin/php: Absolute path to your PHP executable (find this withwhich phpif unsure)/path/to/your/script.php: Absolute path to your PHP script
Save and exit:
In nano, pressCtrl+Oto save, thenCtrl+Xto exit. The cron job will now run automatically on your schedule.
Troubleshooting Cron:
- Test the script manually first with
php /path/to/your/script.phpto confirm it works. - Check cron logs if things break—logs are usually at
/var/log/cron(Debian/Ubuntu) or/var/log/messages(CentOS/RHEL).
内容的提问来源于stack exchange,提问作者user9370375

