1 回答

TA貢獻1790條經驗 獲得超9個贊
在 PHP 中,您可以使用fsockopen,然后在匹配后盡早跳出循環:
<?php
$host = "stackoverflow.com";
$page = "/questions/62504744/is-there-a-way-in-php-to-stop-a-curl-request-once-a-string-match-is-found/62505031";
$fp = fsockopen("ssl://$host", 443, $errno, $errdesc);
if (!$fp)
? ? die("Couldn't connect to $host:\nError: $errno\nDesc: $errdesc\n");
? ??
stream_set_blocking($fp, 0);
$request = "GET $page HTTP/1.1\r\n";
$request .= "Host: $host\r\n";
$request .= "User-Agent: Mozilla/5.0\r\n";
$request .= "Accept: text/xml,application/xml,application/xhtml+xml,";
$request .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
$request .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n\r\n";
fputs ($fp, $request);
$content = '';
while (!feof($fp)) {
? ? $body = fgets($fp);
? ? if (stristr($body, "PHP script that downloads a website's html")) {
? ? ? ? echo 'Was found';
? ? ? ? $content = $body;
? ? ? ? break;
? ? }
}
fclose($fp);
echo $content;
或者,如果你想使用nodejs,你也可以這樣做。
const https = require("https");
const req = https.request({
? host: "stackoverflow.com",
? port: 443,
? path:
? ? "/questions/62504744/is-there-a-way-in-php-to-stop-a-curl-request-once-a-string-match-is-found"
}, function(res) {
? let found = false;
? res.on("data", function(chunk) {
? ? // change PHP script... to DOCTYPE, which will show it aborts after first chunk
? ? if (chunk.includes("PHP script that downloads a website's html")) {
? ? ? found = true;
? ? ? req.abort();
? ? }
? ? console.log(chunk);
? });
? res.on("end", () => console.log(found));
});
req.end();
編輯:
用匹配的字符串做一些事情。
const https = require("https");
// callback function when a match is found
function doSomthing(str){
? console.log('matched partial dom:', str)
}
const req = https.request({
? host: "stackoverflow.com",
? port: 443,
? path:
? ? "/questions/62504744/is-there-a-way-in-php-to-stop-a-curl-request-once-a-string-match-is-found"
}, function(res) {
? let body = ''
? res.on("data", function(chunk) {
? ? // change PHP script... to DOCTYPE, which will show it aborts after first chunk
? ? if (chunk.includes("PHP script that downloads a website's html")) {
? ? ? body = chunk.toString();
? ? ? req.abort();
? ? }
? });
? res.on("end", () => doSomthing(body));
});
req.end();
- 1 回答
- 0 關注
- 125 瀏覽
添加回答
舉報