简体   繁体   English

如何使用php检查网页上是否存在图片/链接?

[英]How can I check if an image/link exist on a page with php?

I'm goging to design benners and stuff for people and then I'll give them an image to put their profiles. 我要为人们设计啤酒瓶和东西,然后给他们一张图片来放置他们的个人资料。 I want to make a page on my localhost to check if image exist on their profile. 我想在本地主机上创建一个页面,以检查其配置文件中是否存在图像。 Basically I want to see if " example1.com/myimg.jpg " exist on " example2.com/theirprofile ". 基本上我想看看“ example2.com/theirprofile ”上是否存在“ example1.com/myimg.jpg ”。

I want to see if the image exists on an external page. 我想查看图像是否存在于外部页面上。 I don't have much experience with php. 我对php没有太多经验。 Is it possible to do sometihng like this? 可以这样做吗?

This is what I usually do: 这通常是我要做的:

The idea is to make a cURL request just to check the result. 这个想法是发出一个cURL请求只是为了检查结果。 If it's 200 then the resource exists. 如果是200,则资源存在。

function externalResourceExists($url) {
    $ch = curl_init($url); //$url is the url you need to check
    curl_setopt($ch, CURLOPT_NOBODY, true); //Don't get the body, we don't need it.
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);                    
    curl_close($ch);        
    return $retcode == 200;
}

Then check if the function returns true. 然后检查函数是否返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM