user2217947
user2217947

Reputation: 37

PHP get meta tags function doesn't work with non http

I was hoping I can get help with a problem I am having.

I'm using the php get meta tags function to see if a tag exist on a list of websites, the problem occurs when ever there is a domain without HTTP.

Ideally I would want to add the HTTP if it doesn't exist, and also I would need a work around if the domain has HTTPS here is the code I'm using.

I will get this error if I land on a site without HTTP in the domain.

Warning: get_meta_tags(www.drhugopavon.com/): failed to open stream: No such file or directory in C:\xampp\htdocs\webresp\index.php on line 16

$urls = array(
    'https://www.smilesbycarroll.com/',
    'https://hurstbournedentalcare.com/',
    'https://www.dentalhc.com/',
    'https://www.springhurstdentistry.com/',
    'https://www.smilesbycarroll.com/',
    'www.drhugopavon.com/'
);

foreach ($urls as $url) {
    $tags = get_meta_tags($url);

    if (isset($tags['viewport'])) {
        echo "$url tag exist" . "</br>";
    }

    if (!isset($tags['viewport'])) {
        echo "$url tag doesnt exist" . "</br>";
    }
}

Upvotes: 0

Views: 443

Answers (3)

user2217947
user2217947

Reputation: 37

you know whats funny, I thought it was because it had http but I put error_reporting(0); in my original code and it worked as I wanted it to haha.

Upvotes: 0

Igor Ilic
Igor Ilic

Reputation: 1368

You can use this to check if the domain has http

foreach($urls as $url){
  if(strpos($url, "http") === FALSE) //check if the url contains http and add it to the beginning of the string if it doesn't 
    $url = "http://" . $url;

  $tags = get_meta_tags($url);
}

Another simpler option would be to check for :// in the url

 foreach($urls as $url){
      if(strpos($url, "://") === FALSE) //check if the url contains http and add it to the beginning of the string if it doesn't 
        $url = "http://" . $url;

      $tags = get_meta_tags($url);
 }

Or you can use regex like Wild Beard suggested

Upvotes: 2

Syscall
Syscall

Reputation: 19764

You could use parse_url() to check if the element scheme exists or not. If not, you could add it:

$urls = array(
    'https://www.smilesbycarroll.com/',
    'https://hurstbournedentalcare.com/',
    'https://www.dentalhc.com/',
    'https://www.springhurstdentistry.com/',
    'https://www.smilesbycarroll.com/',
    'www.drhugopavon.com/'
);

$urls = array_map(function($url) {
  $data = parse_url($url);
  if (!isset($data['scheme'])) $url = 'http://' . $url ;
  return $url;
}, $urls);

print_r($urls);

Upvotes: 2

Related Questions