PHP file_get_contents can't get html code -
i have been using following code html content of webpage
$url = "http://mysmallwebpage.com/"; $html = file_get_contents($url);
however, file_get_contents
can't open urls in following form, if type them in address bar of web browser, these urls open fine.
www.etsy.com/listing/118415624/not-my-small-diary-17-true-high-school blog.al.com/birmingham-news-commentary/2012/11/naked_art_gallery_gives_back_t.html mysmallwebpage.com/
can please tell me can used open above urls in php?
following doesn't work
$url = "www.etsy.com/listing/118415624/not-my-small-diary-17-true-high-school"; $html = file_get_contents($url);
unless not prefix http://
php won't know want webpage , tries on file-system:
$url = "http://www.etsy.com/listing/118415624/not-my-small-diary-17-true-high-school"; $html = file_get_contents($url); if (false === $html) { throw new exception(sprintf('failed open http url "%s".', $url)); }
in development, enable errors, warnings , notices. php tells such problems , appear.
full example (demo):
<?php /** * php file_get_contents can't html code * @link http://stackoverflow.com/q/16118385/367456 */ header('content-type: text/plain'); $url = "http://www.etsy.com/listing/118415624/not-my-small-diary-17-true-high-school"; $html = file_get_contents($url); if (false === $html) { throw new exception(sprintf('failed open http url "%s".', $url)); } $xml = simplexml_import_dom(@domdocument::loadhtml($html), 'simplexmliterator'); $tree = new recursivetreeiterator($xml); foreach($tree $element => $line) { printf("%s <%s>\n", $tree->getprefix(), $element); }
output:
|- <head> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <title> | |- <meta> | |- <link> | |- <link> | |- <link> | |- <link> | |- <link> | |- <meta> | |- <meta> | |- <meta> | |- <link> | |- <link> | |- <link> | |- <link> | |- <link> | |- <link> | |- <link> | |- <script> | |- <script> | |- <script> | |- <script> | |- <script> | |- <script> | |- <link> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | |- <meta> | \- <link> \- <body> |- <div> |- <noscript> | \- <div> | \- <p> | \- <a> |- <script> |- <div> | |- <div> | | |- <ul> | | | |- <li> | | | | \- <a> | | | |- <li> | | | | |- <a> | | | | \- <span> | | | |- <li> | | | | |- <a> | | | | \- <div> | | | | |- <div> | | | | | \- <div> | | | | | \- <ul> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | | \- <a> | | | | | \- <li> | | | | | \- <a> | | | | \- <span> | | | |- <li> | | | | |- <a> | | | | \- <div> | | | | |- <div> | | | | | \- <div> | | | | | \- <ul> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | | \- <a> | | | | | \- <li> | | | | | \- <a> | | | | \- <span> | | | |- <li> | | | | \- <a> | | | |- <li> | | | | \- <a> | | | \- <li> | | | \- <a> | | \- <ul> | | \- <li> | | \- <a> | \- <div> | |- <h1> | | \- <a> | |- <ul> | | |- <li> | | | \- <a> | | \- <li> | | \- <a> | |- <input> | |- <input> | |- <input> | |- <input> | |- <input> | |- <form> | | |- <div> | | | |- <input> | | | |- <button> | | | \- <div> | | | \- <ul> | | |- <input> | | \- <input> | \- <div> | \- <a> | \- <span> |- <div> | \- <div> | |- <a> | | \- <span> | |- <ul> | | |- <li> | | | \- <a> | | \- <li> | | \- <a> | \- <div> | |- <div> | | |- <div> | | | \- <p> | | |- <div> | | \- <form> | | |- <div> | | | \- <h2> | | |- <div> | | | |- <div> | | | | |- <a> | | | | | \- <span> | | | | \- <div> | | | |- <input> | | | |- <input> | | | |- <input> | | | |- <input> | | | \- <p> | | |- <div> | | | \- <span> | | |- <div> | | | |- <label> | | | |- <span> | | | |- <span> | | | \- <input> | | |- <div> | | | |- <label> | | | |- <span> | | | \- <input> | | |- <div> | | | |- <input> | | | |- <label> | | | |- <input> | | | |- <label> | | | |- <input> | | | \- <label> | | |- <hr> | | |- <div> | | | |- <label> | | | |- <span> | | | | |- <span> | | | | \- <span> | | | |- <span> | | | \- <input> | | |- <div> | | | |- <label> | | | |- <span> | | | \- <input> | | |- <div> | | | |- <label> | | | |- <span> | | | \- <input> | | |- <div> | | | |- <label> | | | |- <span> | | | |- <input> | | | |- <div> | | | | |- <span> | | | | \- <span> | | | \- <div> | | | |- <span> | | | \- <span> | | |- <hr> | | |- <p> | | | |- <a> | | | \- <a> | | |- <p> | | | |- <input> | | | |- <input> | | | |- <input> | | | |- <span> | | | | \- <span> | | | | \- <input> | | | \- <span> | | \- <div> | | |- <input> | | \- <label> | \- <div> | |- <div> | | \- <p> | \- <form> | |- <div> | | |- <label> | | |- <input> | | \- <span> | |- <div> | | |- <label> | | |- <input> | | \- <span> | |- <div> | | |- <input> | | \- <label> | |- <p> | | |- <input> | | |- <input> | | |- <input> | | |- <span> | | | \- <span> | | | \- <input> | | \- <span> | |- <hr> | |- <p> | | \- <a> | \- <p> | \- <a> |- <div> | \- <div> | \- <div> |- <script> |- <script> |- <div> | \- <div> | \- <p> | |- <strong> | |- <strong> | \- <strong> |- <div> | |- <ul> | | |- <li> | | | |- <a> | | | \- <span> | | \- <li> | | \- <a> | \- <div> | \- <div> | |- <div> | | |- <div> | | | |- <div> | | | | |- <input> | | | | |- <div> | | | | | \- <a> | | | | | \- <span> | | | | \- <div> | | | | |- <h3> | | | | \- <p> | | | |- <div> | | | | |- <h1> | | | | \- <p> | | | | \- <a> | | | |- <div> | | | | |- <div> | | | | | \- <a> | | | | | \- <img> | | | | \- <div> | | | | \- <a> | | | |- <div> | | | | |- <a> | | | | | \- <img> | | | | |- <a> | | | | | \- <img> | | | | |- <a> | | | | | \- <img> | | | | |- <a> | | | | | \- <img> | | | | \- <a> | | | | \- <img> | | | |- <div> | | | | \- <div> | | | | |- <br> | | | | \- <p> | | | | |- <br> | | | | \- <a> | | | |- <div> | | | | |- <h3> | | | | | \- <span> | | | | |- <h4> | | | | \- <table> | | | | |- <tr> | | | | | |- <th> | | | | | |- <th> | | | | | \- <th> | | | | |- <tr> | | | | | |- <td> | | | | | |- <td> | | | | | | |- <span> | | | | | | |- <span> | | | | | | \- <span> | | | | | \- <td> | | | | | |- <span> | | | | | |- <span> | | | | | \- <span> | | | | \- <tr> | | | | |- <td> | | | | |- <td> | | | | | |- <span> | | | | | |- <span> | | | | | \- <span> | | | | \- <td> | | | | |- <span> | | | | |- <span> | | | | \- <span> | | | |- <div> | | | | |- <div> | | | | | |- <h3> | | | | | \- <ul> | | | | | |- <div> | | | | | | |- <p> | | | | | | | \- <span> | | | | | | \- <span> | | | | | |- <li> | | | | | | \- <span> | | | | | \- <li> | | | | | \- <span> | | | | \- <div> | | | | |- <h3> | | | | \- <ul> | | | | |- <li> | | | | | \- <a> | | | | \- <li> | | | | \- <a> | | | |- <div> | | | | |- <ul> | | | | | |- <li> | | | | | | \- <a> | | | | | | \- <strong> | | | | | | |- <span> | | | | | | \- <span> | | | | | \- <li> | | | | | |- <div> | | | | | | \- <a> | | | | | \- <div> | | | | \- <div> | | | | |- <div> | | | | | |- <div> | | | | | | |- <h3> | | | | | | |- <p> | | | | | | |- <h3> | | | | | | \- <p> | | | | | \- <div> | | | | | |- <h3> | | | | | \- <p> | | | | |- <div> | | | | | \- <div> | | | | | |- <h3> | | | | | \- <div> | | | | | |- <a> | | | | | |- <a> | | | | | \- <a> | | | | \- <div> | | | | \- <ul> | | | | |- <li> | | | | \- <li> | | | |- <div> | | | | |- <h3> | | | | \- <div> | | | | \- <ul> | | | \- <script> | | |- <div> | | | |- <div> | | | | |- <div> | | | | | |- <div> | | | | | |- <div> | | | | | | |- <span> | | | | | | |- <span> | | | | | | |- <a> | | | | | | | \- <span> | | | | | | \- <span> | | | | | \- <div> | | | | \- <div> | | | | |- <div> | | | | |- <div> | | | | | \- <div> | | | | | \- <div> | | | | \- <div> | | | | |- <div> | | | | | \- <form> | | | | | |- <input> | | | | | |- <input> | | | | | |- <input> | | | | | |- <input> | | | | | |- <input> | | | | | \- <span> | | | | | \- <span> | | | | | \- <input> | | | | \- <div> | | | | \- <div> | | | | |- <input> | | | | |- <input> | | | | |- <input> | | | | \- <a> | | | | \- <span> | | | |- <div> | | | | \- <p> | | | | |- <span> | | | | \- <span> | | | | \- <span> | | | |- <script> | | | |- <div> | | | | |- <div> | | | | | |- <h2> | | | | | |- <ul> | | | | | | |- <li> | | | | | | | \- <span> | | | | | | | \- <a> | | | | | | \- <li> | | | | | |- <div> | | | | | | \- <div> | | | | | | |- <span> | | | | | | |- <div> | | | | | | | \- <a> | | | | | | | \- <img> | | | | | | |- <div> | | | | | | | \- <a> | | | | | | | \- <img> | | | | | | \- <div> | | | | | | \- <a> | | | | | | \- <span> | | | | | \- <ul> | | | | | |- <li> | | | | | | |- <input> | | | | | | \- <a> | | | | | | \- <span> | | | | | \- <li> | | | | | \- <a> | | | | | \- <span> | | | | |- <div> | | | | | |- <h2> | | | | | \- <div> | | | | | |- <div> | | | | | | \- <a> | | | | | | \- <img> | | | | | \- <ul> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | | \- <a> | | | | | |- <li> | | | | | | \- <a> | | | | | \- <li> | | | | | \- <a> | | | | | \- <span> | | | | \- <div> | | | | |- <h2> | | | | |- <ul> | | | | | |- <li> | | | | | | |- <input> | | | | | | \- <a> | | | | | | |- <span> | | | | | | \- <span> | | | | | |- <li> | | | | | | \- <a> | | | | | | \- <span> | | | | | \- <li> | | | | | \- <a> | | | | | |- <span> | | | | | \- <span> | | | | |- <hr> | | | | |- <ul> | | | | | |- <li> | | | | | | |- <script> | | | | | | \- <a> | | | | | | |- <span> | | | | | | \- <span> | | | | | |- <li> | | | | | | |- <script> | | | | | | \- <a> | | | | | \- <li> | | | | | |- <like> | | | | | \- <script> | | | | |- <hr> | | | | \- <div> | | | | \- <ul> | | | | |- <li> | | | | \- <li> | | | | \- <a> | | | \- <div> | | | |- <div> | | | | |- <div> | | | | |- <div> | | | | | |- <span> | | | | | |- <span> | | | | | |- <a> | | | | | | \- <span> | | | | | \- <span> | | | | \- <div> | | | \- <div> | | | |- <div> | | | |- <div> | | | | \- <div> | | | | \- <div> | | | \- <div> | | | |- <div> | | | | \- <form> | | | | |- <input> | | | | |- <input> | | | | |- <input> | | | | |- <input> | | | | |- <input> | | | | \- <span> | | | | \- <span> | | | | \- <input> | | | \- <div> | | | \- <div> | | | |- <input> | | | |- <input> | | | |- <input> | | | \- <a> | | | \- <span> | | |- <script> | | |- <div> | | | \- <div> | | | |- <span> | | | \- <a> | | | \- <span> | | \- <script> | |- <div> | \- <div> | |- <div> | | |- <div> | | | |- <h2> | | | \- <a> | | | \- <span> | | \- <form> | | |- <div> | | | |- <div> | | | |- <div> | | | |- <input> | | | |- <div> | | | |- <div> | | | | |- <label> | | | | \- <select> | | | | |- <option> | | | | |- <optgroup> | | | | \- <optgroup> | | | \- <div> | | | |- <label> | | | \- <input> | | \- <div> | | |- <button> | | \- <div> | | \- <span> | \- <div> | \- <div> | |- <a> | | \- <span> | |- <h2> | \- <a> |- <div> |- <div> | |- <p> | |- <ul> | | |- <li> | | | \- <a> | | |- <li> | | | \- <a> | | |- <li> | | | \- <a> | | |- <li> | | | \- <a> | | |- <li> | | | \- <a> | | |- <li> | | | \- <a> | | |- <li> | | | \- <a> | | |- <li> | | | \- <a> | | \- <li> | | \- <a> | |- <p> | | |- <a> | | | \- <span> | | |- <a> | | | \- <span> | | |- <a> | | \- <span> | \- <script> |- <script> |- <script> |- <div> | \- <div> | |- <div> | | \- <h2> | \- <div> | \- <div> | \- <a> | \- <span> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <noscript> | \- <iframe> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> |- <script> \- <script>
Comments
Post a Comment