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

旧巷老猫 提交于 2019-12-12 02:55:31

问题


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


回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/20310892/getting-raw-output-in-local-wamp-same-code-working-fine-in-webserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!