[英]Can I tell my php function to not execute until the "file_get_contents" has loaded the page using javascript in php code?
This is my php function:这是我的 php 函数:
$result = file_get_contents('https://www.example.com/index.php');
$regex = '/<img class="pic" src="(.*?)">/';
preg_match($regex, $result, $matches);
return($matches[1]);
it's a function to get a picture from that link, sometimes it return the picture, sometimes not, because the image in the site sometimes take seconde to load sometimes not.这是从该链接获取图片的功能,有时它返回图片,有时不返回,因为站点中的图像有时需要第二次加载有时不会。 I want to prevent that from happening in my function, I want to delay the return until the image is loaded, i know this is not possible with php, sleep() didn't help, so I know that I must do it with javascript, but I don't know how to do it, please help ty!
我想防止在我的函数中发生这种情况,我想延迟返回直到图像加载,我知道这用 php 是不可能的,sleep() 没有帮助,所以我知道我必须用 javascript 来做,但我不知道该怎么做,请帮助您!
If you'd like to load something asynchronously, I guess the simplest solution would be to launch another request from the client side, eg:如果您想异步加载某些内容,我想最简单的解决方案是从客户端启动另一个请求,例如:
fetch-image.php
for example.fetch-image.php
。Content-Type
header.Content-Type
标头。 Samplescan be found here .src="/fetch-image.php"
attribute with the path to the fetch scriptsrc="/fetch-image.php"
属性替换为获取脚本的路径Modified 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]);
Hope that helps.希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.