为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 有没有办法在找到字符串匹配后停止curl 请求?

PHP 有没有办法在找到字符串匹配后停止curl 请求?

PHP
蝴蝶不菲 2023-06-24 15:37:50
我目前有一个 PHP 脚本,可以下载网站的 html,然后在结果preg_match上运行curl_exec()。该网页大小约为 2 Mb,并且匹配字符串通常位于页面开头,因此大量的下载时间似乎是不必要的。我想知道是否有办法在找到字符串后终止卷曲会话。管道有用吗?我也愿意尝试其他框架,例如 BASH 和 Javascript。谢谢。
查看完整描述

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();


查看完整回答
反对 回复 2023-06-24
  • 1 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信