php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64946 DomDocument getAttribute return empty on IMG/src, LINK/href, with protocol-rela
Submitted: 2013-05-29 22:29 UTC Modified: 2013-05-29 22:55 UTC
From: work at danemacmillan dot com Assigned:
Status: Closed Package: *XML functions
PHP Version: 5.4.15 OS: centos6.4 (64bit), win7 (64bit)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: work at danemacmillan dot com
New email:
PHP Version: OS:

 

 [2013-05-29 22:29 UTC] work at danemacmillan dot com
Description:
------------
DomDocument's getAttribute will return an empty string on both an IMG tag's SRC attribute, and a LINK's HREF attribute *when* the URLs provided are protocol-relative. 

In the description below I'm going to refer to IMG tags only, but the same issue stands for the LINK tag's HREF attribute as well. There may be others, but these are the only two I discovered. 

The problem arises when scraping the SRC of IMG tags. If the IMG tag SRC has a protocol-relative URL (an absolute path beginning with "//" instead of "http://"), it will be unreadable. Relative paths are readable (e.g., "/img/landing.png").

I've used both the getElementsByTagName method and the Xpath method. They both suffer from the same problem. However, the moment I prepend any absolute URL with its designated protocol, the IMG SRC is readable. 

This problem does not exist for the A tag HREF attribute, nor the SCRIPT tag SRC attribute; in each case the URL provided will be returned, regardless of the URL format.

Test script:
---------------
To summarize, the first two URLs will be readable, and the third URL will *not* be readable:

<img src="http://www.example.com/img/eg.png" />
<img src="/img/eg.png" />
<img src="//www.example.com/img/eg.png" />

I'll demonstrate two ways to grab the URLs, and they both fail with protocol-relative URLs.

// use for both examples:
$html = file_get_contents("http://www.example.com");

// one way (Xpath)
$dom = new DOMDocument();
@$dom->loadHTML($html);
$x = new DOMXPath($dom); 
$data = array();
foreach($x->query('//img') as $node) {
    $data['img']['src'][] = urldecode($node->getAttribute('src'));
}

// another way (getElementsByTagName)
$doc = new DOMDocument();
@$doc->loadHTML($html);
$imgs = $doc->getElementsByTagName('img');
$data = array();
for ($i = 0; $i < $imgs->length; $i++) {
    $img = $imgs->item($i);
    if($img->getAttribute('src'))
    {
        $data[] = urldecode($img->getAttribute('src'));
    }
} 

// print results from either
print_r($data);

Expected result:
----------------
Array ( [img] => Array ( [src] => Array ( [0] => //www.example.com/img/eg.png ) ) )

Actual result:
--------------
Array ( [img] => Array ( [src] => Array ( [0] =>  ) ) )

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-29 22:55 UTC] work at danemacmillan dot com
-Status: Open +Status: Closed
 [2013-05-29 22:55 UTC] work at danemacmillan dot com
I don't know how, but the exact same code did not work for two days. After reporting a bug, it works.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Aug 03 10:01:24 2020 UTC