0

trouble reading yql file

Hi

I am using PHP to read a a YQL file. Problem is it outputs all the results as a big string. I would really appreciate some help on how to put it in an array. Feel like I am so close yet so far!!

Fiz


<?php

$dom2 = new DOMDocument('1.0', 'UTF-8');

$search2 = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%20in%20(%22m1%22%2C%20%22m2%22)&format=xml';
$xml2 = file_get_contents($search2);
$dom2 ->loadXML($xml2);
$res2 = xml_to_result($dom2);
print_r ($res2['results']);

function xml_to_result($dom) {
$root = $dom->firstChild;
foreach($root->attributes as $attr) $res[$attr->name] = $attr->value ;
$node = $root->firstChild;
$i = 0;
while($node) {
switch($node->nodeName) {
case 'Result':
$subnode = $node->firstChild;
while($subnode) {
$subnodes = $subnode->childNodes;
foreach($subnodes as $n) {
if($n->hasChildNodes()) {
foreach($n->childNodes as $cn)
{
$res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue) ;
}
} else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
}
$subnode = $subnode->nextSibling;
}
break;
case 'results':
default:
$res[$node->nodeName] = trim($node->nodeValue) ;
$i--;
break;
}
$i++;
$node = $node->nextSibling;
}
return $res;
}



?>

5 Replies
  • Hi fizzzy1,

    I just wanted to post a note to let you know that I'm putting together some samples that will help you out with this. I'll try to get back to you on this asap.

    Jonathan LeBlanc
    Senior Software Engineer
    Yahoo! Developer Network
    Twitter: jcleblanc

    QUOTE (fizzzy1 @ Jul 2 2009, 09:52 AM) <{POST_SNAPBACK}>
    Hi

    I am using PHP to read a a YQL file. Problem is it outputs all the results as a big string. I would really appreciate some help on how to put it in an array. Feel like I am so close yet so far!!

    Fiz


    <?php

    $dom2 = new DOMDocument('1.0', 'UTF-8');

    $search2 = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%20in%20(%22m1%22%2C%20%22m2%22)&format=xml';
    $xml2 = file_get_contents($search2);
    $dom2 ->loadXML($xml2);
    $res2 = xml_to_result($dom2);
    print_r ($res2['results']);

    function xml_to_result($dom) {
    $root = $dom->firstChild;
    foreach($root->attributes as $attr) $res[$attr->name] = $attr->value ;
    $node = $root->firstChild;
    $i = 0;
    while($node) {
    switch($node->nodeName) {
    case 'Result':
    $subnode = $node->firstChild;
    while($subnode) {
    $subnodes = $subnode->childNodes;
    foreach($subnodes as $n) {
    if($n->hasChildNodes()) {
    foreach($n->childNodes as $cn)
    {
    $res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue) ;
    }
    } else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
    }
    $subnode = $subnode->nextSibling;
    }
    break;
    case 'results':
    default:
    $res[$node->nodeName] = trim($node->nodeValue) ;
    $i--;
    break;
    }
    $i++;
    $node = $node->nextSibling;
    }
    return $res;
    }



    ?>
    0
  • Alright, here's a sample that should help. What I'm doing is returning JSON instead of XML, which I find easier to work with personally. The returned JSON is a serialized string, so we use json_decode to decode it to a PHP object, then we capture the results as an array. $arrPlaces will now house an array with two elements, which are the results.

    CODE
    <?php
    $search = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%20in%20(%22m1%22%2C%20%22m2%22)&format=json';
    $results = json_decode(file_get_contents($search));
    $arrPlaces = $results->query->results->place;
    ?>


    Let me know if you need anything else,
    Jon
    0
  • Thanks Jonathan

    Look forward to seeing this.
    In the end I just created a function to split results based on their patterns. Not ideal, be great to find out the real way to do this when do you think it might be available?

    Farzana

    QUOTE (Jon @ Jul 7 2009, 02:02 PM) <{POST_SNAPBACK}>
    Hi fizzzy1,

    I just wanted to post a note to let you know that I'm putting together some samples that will help you out with this. I'll try to get back to you on this asap.

    Jonathan LeBlanc
    Senior Software Engineer
    Yahoo! Developer Network
    Twitter: jcleblanc
    0
  • Thanks, must have been replying at the same time as you, I'll check it out.

    Fiz

    QUOTE (fizzzy1 @ Jul 7 2009, 04:25 PM) <{POST_SNAPBACK}>
    Thanks Jonathan

    Look forward to seeing this.
    In the end I just created a function to split results based on their patterns. Not ideal, be great to find out the real way to do this when do you think it might be available?

    Farzana
    0
  • if you have php5 you can use simplexml to handle the xml file.
    You can do something like this :
    CODE
    $xmlobj = simplexml_load_file("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22abc%22%20limit%205&format=xml");

    and then you can use it like an object.
    0

Recent Posts

in YQL