Click here to Skip to main content
15,170,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
I am rookie in php and need some help.

what i am trying to do is to extract the url which is as follows:
http://www.weatherzone.co.nz/north-island/wellington

However i am not getting the image tags.

i have done the following.
PHP
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.weatherzone.co.nz/north-island/wellington');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;

$url="http://www.weatherzone.co.nz/north-island/wellington";

$html = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName('img');

foreach ($tags as $tag) {
  echo $tag->getAttribute('src');
}

This code only gets me the website content but doesnt shows the images from the external url.
Any help would be appreciated!!!
Thanks in advance.
Posted
Updated 15-Dec-14 20:50pm
v2
Comments
Chubby Ninja 15-Dec-14 17:37pm
   
For some reason I get an error when posting my answer, anyway, the reason you arent getting any results is because weatherzone is blocking file_get_contents.. use cURL and imitate a web browser:

$url="http://www.weatherzone.co.nz/north-island/wellington";
$agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$html = curl_exec($ch);

$doc = new DOMDocument();
$doc->loadHTML($html); // don't suppress errors

$tags = $doc->getElementsByTagName('img');

foreach ($tags as $tag) {
echo $tag->getAttribute('src');
}
Amit Karyekar 15-Dec-14 17:48pm
   
it doesnt work out
gives an output of
wellington/images/logos/weatherzone_logo_185x36.png/images/widgets
however earlier it was showing the text on web.

As you can see image sources are relative URLs in that site, like '/images/logos/weatherzone_logo_185x36.png'...
When you display it in the context of your site it will not be found by the browser as the final URL fill be somethng like 'http://my-site/images/logos/weatherzone_logo_185x36.png', while on the original site it will be 'http://www.weatherzone.co.nz/wellington/images/logos/weatherzone_logo_185x36.png'...
   
the reason you arent getting any results is because weatherzone is blocking file_get_contents().. use cURL and imitate a web browser:

PHP
$url="http://www.weatherzone.co.nz/north-island/wellington";
$agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30';
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$html = curl_exec($ch);
 
$doc = new DOMDocument();
$doc->loadHTML($html); // don't suppress errors

$tags = $doc->getElementsByTagName('img');

foreach ($tags as $tag) {
echo $tag->getAttribute('src');
}


This prints out the src="" as you've said, so you need to wrap that in img tags and reference their domain - as the images are stored on their website not yours..
PHP
foreach ($tags as $tag) {
echo '<img src="http://www.weatherzone.co.nz' . $tag->getAttribute('src') . '">';
}
   
v2
Comments
Amit Karyekar 16-Dec-14 14:22pm
   
hey chubby are the tags properly closed.it posts a compilation error.
Chubby Ninja 17-Dec-14 2:35am
   
heya Amit,

sorry about that - I've fixed it now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900