Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

php - relative url not working for getheaders

here is this script which works just fine for absolute url in img src tag but if img src tag is having relative url it fails and give error

Warning: get_headers() [function.get-headers]: This function may only be used against URLs in in first link img src is relative url but in second link img src is absolute url and it gives perfect result but i want my script to work in both cases any idea??

<?php
$websitelink = 'http://img172.imagevenue.com/img.php?image=90465_Gwen3_122_17lo.jpg';

//  $websitelink='http://www.santabanta.com/bollywood/76432/whoa-after-200-crore-now-dhoom-3-races-for-rs-300-crore-mark/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $websitelink);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resulty = curl_exec($ch);
$doc = new DOMDocument();
@$doc->loadHTML($resulty);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
    $data = get_headers($tag->getAttribute('src'), 1);
    if ((intval($data["Content-Length"])) >= 30720) {
        $op7 = '' . $tag->getAttribute('src') . '';
        echo $op7 . '</br>';
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function get_headers will actually perform a HTTP GET request, therefore it needs a fully qualified address, and cannot work if you specify only a relative URL.

You will have to infer what's the complete address to the image you're trying to load.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...