insideitem) { $this->tag = $tagName; } elseif ($tagName == "RESULT") { $this->insideitem = true; } if ($tagName == "CACHE") { $this->insideitem = false; } } function endElement($parser, $tagName) { if ($tagName == "RESULT") { printf("%s
", $this->url,$this->title); $this->title = ""; $this->url = ""; $this->summary = ""; $this->insideitem = false; } } function characterData($parser, $data) { if ($this->insideitem) { switch ($this->tag) { case "TITLE": $this->title .= $data; break; case "URL": $this->url .= $data; break; case "SUMMARY": $this->summary .= $data; break; } } } } $xml_parser = xml_parser_create(); $yahoo_parser = new YahooParser(); xml_set_object($xml_parser,&$yahoo_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=madonna&results=2","r") or die("Error reading Yahoo XML data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>