Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-检查链接中是否存在URL_Php_Html_Url - Fatal编程技术网

PHP-检查链接中是否存在URL

PHP-检查链接中是否存在URL,php,html,url,Php,Html,Url,这是我想做的 让我们假设我正在一个位于的文件中查找链接“example.com” 我想用一个PHP脚本在上面提到的网站中查找一个。但是,如果中有类或ID标记,我也需要它来工作。以下是我发现的,以防其他人也需要它 $url = "http://example.com/test.html"; $searchFor = "example.com" $input = @file_get_contents($url) or die("Could not access file: $url");

这是我想做的

让我们假设我正在一个位于的文件中查找链接“example.com”


我想用一个PHP脚本在上面提到的网站中查找一个。但是,如果

中有类或ID标记,我也需要它来工作。以下是我发现的,以防其他人也需要它

  $url = "http://example.com/test.html";
  $searchFor = "example.com"
  $input = @file_get_contents($url) or die("Could not access file: $url");
  $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
  if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
      echo $match[2];
      if ($match[2] == $searchFor)
      {
          $isMatch = 1;
      } else {
          $isMatch= 0;
      }
      // $match[0] = A tag
      // $match[2] = link address
      // $match[3] = link text
    }
  }

      if ($isMatch)
      {
          echo "<p><font color=red size=5 align=center>The page specified does contain your link. You have been credited the award amount!</font></p>";
      } else {
          echo "<p><font color=red size=5 align=center>The specified page does not have your referral link.</font></p>";
      }
$url=”http://example.com/test.html";
$searchFor=“example.com”
$input=@file\u get\u contents($url)或die(“无法访问文件:$url”);
$regexp=“]*href=(\”?)([^\“>]*?)\\1[^>]*>(.*);
if(预匹配全部(“/$regexp/siU”、$input、$matches、预设置顺序)){
foreach($matches作为$match进行匹配){
echo$match[2];
如果($match[2]==$searchFor)
{
$isMatch=1;
}否则{
$isMatch=0;
}
//$match[0]=标记
//$match[2]=链接地址
//$match[3]=链接文本
}
}
如果($isMatch)
{
echo“指定的页面不包含您的链接。您已获得奖励金额!

”; }否则{ echo“指定页面没有您的推荐链接。

”; }
请参见下面的url

或者试试看

$file = 'http://www.domain.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}
从这里开始:

…在上面的柱子正下方,有一个卷曲解决方案:

function url_exists($url) {
    if (!$fp = curl_init($url)) return false;
    return true;
}
更新代码:-

您可以使用SimpleHtmlDom类来查找id或标记中的类

请参阅下面的URL


关于连接超时(-code)、403和301s呢?我认为更好的方法是检查http响应代码是否为200?真:假。不是看url是否存在,而是看a中的HREF是否等于“Example.com”“这些方法是合理的,但可能不适用于主机服务器重定向的URL。在这些情况下,您可能希望检查get_headers结果中的最终“Location:…”条目。