【问题标题】:How can I check if an image/link exist on a page with php?如何使用 php 检查页面上是否存在图像/链接?
【发布时间】:2016-07-24 04:03:29
【问题描述】:

我要为人们设计本纳和其他东西,然后我会给他们一张图片来放置他们的个人资料。我想在我的本地主机上创建一个页面来检查他们的个人资料中是否存在图像。基本上我想看看“example2.com/theirprofile”上是否存在“example1.com/myimg.jpg”。

我想查看图像是否存在于外部页面上。 我对php没有太多经验。有可能做这样的事情吗?

【问题讨论】:

标签: php


【解决方案1】:

这是我通常做的:

这个想法是发出一个 cURL 请求来检查结果。如果是 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;
}

然后检查函数是否返回true。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    相关资源
    最近更新 更多