0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Priya
  • 165
  • 1
  • 12
  • What is the value of `$op7` when it is echoed? Perhaps it is a relative url? – Sarah Kemp Dec 02 '13 at 16:08
  • @SarahKemp i have got some clue the moment i use -> it starts giving raw code as output @$doc->loadHTML($html); – Priya Dec 02 '13 at 16:25
  • Please explain your comment further by editing your question and answer my question if you want more help. It would also help to remove the `@` sign in your statement so we can see your errors. – Sarah Kemp Dec 02 '13 at 18:07
  • @SarahKemp here is the screenshot of my errors http://i39.tinypic.com/2417ar5.png AND http://i43.tinypic.com/eq1l36.png – Priya Dec 03 '13 at 16:56

2 Answers2

0

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;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

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.

Sarah Kemp
  • 2,670
  • 3
  • 21
  • 29
  • But it is not working on your local server or you wouldn't be asking for help. Did you try my suggestion in this answer? Did you remove the error suppressor from your `loadHTML()` request? I'm not going to do your research for you if you won't even try these things. – Sarah Kemp Dec 04 '13 at 16:01