简体   繁体   中英

getting raw output in local wamp same code working fine in webserver

i have this script which is workign just fine in server but in local wamp it is giving error

i have fopen is on

Warning: get_headers(): This function may only be used against URLs in C:\wamp\www\url\test5.php on line 8


<?php 
    $websitelink= 'http://www.brobible.com/girls/article/miley-cyrus-21st-birthday-party';
    $html = file_get_contents($websitelink); 
    $doc = new DOMDocument(); 
    @$doc->loadHTML($html); 
    $tags = $doc->getElementsByTagName('img'); 
    foreach ($tags as $tag) { 
        $data = get_headers($tag->getAttribute('src'),1); 
        $op7=''.$tag->getAttribute('src').'';
        echo $op7;
    }
?>

this code just works fine in server but not in local wamp server

I think you need to turn on the following PHP parameter.

allow_url_fopen = On

You can find this in the php.ini file.

If you are using WAMPServer then you can also turn this on using the wampmanager icon menus as follows

left_click wampmanager icon -> PHP -> PHP Settings -> Allow URL Fopen

I had a closer look at your code.

I would imagine that the data in $tag->getAttribute('src') does not have a full url at least in one case which is causing your error. It is probably using a relative address like img/imagename.png and not http://example.com/img/imagename.png

That would explain the error message nicely.

Try adding an echo of what you are getting out of that statement.

$tags = $doc->getElementsByTagName('img'); 
foreach ($tags as $tag) { 

    echo $tag->getAttribute('src');

    $data = get_headers($tag->getAttribute('src'),1); 
    $op7=''.$tag->getAttribute('src').'';
    echo $op7;
}

The value of $op7 is /files/img/nav/nav_02.png according the the screenshot you linked. That is a root-relative URL, get_headers() requires an absolute URL (starting with 'http://').

You need to glue the domain you are querying ( http://www.brobible.com ) to the root-relative path of the image so that it looks like

get_headers('http://www.brobible.com'.$tag->getAttribute('src'),1)

Keep in mind this will now only work with root-relative paths; you will probably want to check for absolute and relative paths before you assume they need the domain glued on this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM