472,146 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

SimpleXML & Google images

Hi, I'm writing a script that would (hopefully) search google images
for whatever, and then return a list of URLs for the image. Right now I
have:

$dom = new DomDocument();

$url = "http://images.google.com/images?q=hello";

$dom->loadHTMLFile($url);

$sxe = simplexml_import_dom($dom);
foreach($sxe->xpath('//img') as $node)
{
echo $node->src;
}

What this is supposed to do (line by line):
Make a new DomDocument
Define $url
load the HTML file from $url
make a simpleXML object from the loaded DOM
go through all the image tags (using a DomNodeList w/ xpath) and print
out the src attribute of the node.

What it does:
Prints nothing.

Just wondering if anyone knew what ws goin on.
Thanks.

Sep 8 '06 #1
2 3711

mouseit101 wrote:
Hi, I'm writing a script that would (hopefully) search google images
for whatever, and then return a list of URLs for the image. Right now I
have:

$dom = new DomDocument();

$url = "http://images.google.com/images?q=hello";

$dom->loadHTMLFile($url);

$sxe = simplexml_import_dom($dom);
foreach($sxe->xpath('//img') as $node)
{
echo $node->src;
}

What this is supposed to do (line by line):
Make a new DomDocument
Define $url
load the HTML file from $url
make a simpleXML object from the loaded DOM
go through all the image tags (using a DomNodeList w/ xpath) and print
out the src attribute of the node.

What it does:
Prints nothing.

Just wondering if anyone knew what ws goin on.
Thanks.
It prints nothing because you have error_reporting disabled in php.ini.
If you enable it you'll see what's going on. For example, here's what
happened when I ran your code:

PHP Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : head in
http://images.google.com/images?q=hello, line: 32 in foo.php on line 7

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : head in
http://images.google.com/images?q=hello, line: 32 in foo.php on line 7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseStartTag: misplaced
<bodytag in http://images.google.com/images?q=hello, line: 32 in
foo.phpon line 7

Warning: DOMDocument::loadHTMLFile(): htmlParseStartTag: misplaced
<bodytag in http://images.google.com/images?q=hello, line: 32 in
foo.php on line 7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef:
expecting ';' in http://images.google.com/images?q=hello, line: 32 in
foo.php on line 7

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';'
in http://images.google.com/images?q=hello, line: 32 in foo.php on line
7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef:
expecting ';' in http://images.google.com/images?q=hello, line: 32 in
foo.php on line 7

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';'
in http://images.google.com/images?q=hello, line: 32 in foo.php on line
7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef:
expecting ';' in http://images.google.com/images?q=hello, line: 32 in
/foo.php on line 7

Sep 8 '06 #2

ZeldorBlat wrote:
mouseit101 wrote:
Hi, I'm writing a script that would (hopefully) search google images
for whatever, and then return a list of URLs for the image. Right now I
have:

$dom = new DomDocument();

$url = "http://images.google.com/images?q=hello";

$dom->loadHTMLFile($url);

$sxe = simplexml_import_dom($dom);
foreach($sxe->xpath('//img') as $node)
{
echo $node->src;
}

What this is supposed to do (line by line):
Make a new DomDocument
Define $url
load the HTML file from $url
make a simpleXML object from the loaded DOM
go through all the image tags (using a DomNodeList w/ xpath) and print
out the src attribute of the node.

What it does:
Prints nothing.

Just wondering if anyone knew what ws goin on.
Thanks.

It prints nothing because you have error_reporting disabled in php.ini.
If you enable it you'll see what's going on. For example, here's what
happened when I ran your code:

PHP Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : head in
http://images.google.com/images?q=hello, line: 32 in foo.php on line 7

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : head in
http://images.google.com/images?q=hello, line: 32 in foo.php on line 7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseStartTag: misplaced
<bodytag in http://images.google.com/images?q=hello, line: 32 in
foo.phpon line 7

Warning: DOMDocument::loadHTMLFile(): htmlParseStartTag: misplaced
<bodytag in http://images.google.com/images?q=hello, line: 32 in
foo.php on line 7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef:
expecting ';' in http://images.google.com/images?q=hello, line: 32 in
foo.php on line 7

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';'
in http://images.google.com/images?q=hello, line: 32 in foo.php on line
7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef:
expecting ';' in http://images.google.com/images?q=hello, line: 32 in
foo.php on line 7

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';'
in http://images.google.com/images?q=hello, line: 32 in foo.php on line
7
PHP Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef:
expecting ';' in http://images.google.com/images?q=hello, line: 32 in
/foo.php on line 7

Ok, Anyone know how to fix it?

Sep 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mr WZ Boson | last post: by
2 posts views Thread by mandric | last post: by
1 post views Thread by Andy | last post: by
1 post views Thread by =?iso-8859-1?Q?=C1lvaro?= G. Vicario | last post: by
1 post views Thread by duane.lortie | last post: by
anfetienne
2 posts views Thread by anfetienne | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.