这是我的php函数:
$result = file_get_contents('https://www.example.com/index.php');
$regex = '/<img class="pic" src="(.*?)">/';
preg_match($regex, $result, $matches);
return($matches[1]);这是一个从链接中获取图片函数,它有时返回图片,有时不返回,因为站点中的图像有时需要几秒钟才能加载,有时不需要。我想防止在我的函数中发生这种情况,我想延迟返回,直到图像加载,我知道这在php中是不可能的,sleep()没有帮助,所以我知道我必须用javascript来做,但我不知道怎么做,请帮帮我!
发布于 2020-02-27 14:47:23
如果你想异步加载一些东西,我想最简单的解决方案是从客户端发起另一个请求,例如:
fetch-image.php中调用。Content-Type头。示例can be found here。对于需要修改的代码,请参见下面的内容。将上面提到的代码移动到新的scriptsrc="/fetch-image.php"属性,其中包含fetch script的路径
修改后的脚本:
$result = file_get_contents('https://www.example.com/index.php');
$regex = '/<img class="pic" src="(.*?)">/';
preg_match($regex, $result, $matches);
echo file_get_contents($matches[1]);希望这能有所帮助。
https://stackoverflow.com/questions/60427033
复制相似问题