0

Here is the script I currently am utilizing:

$mysongs = simplexml_load_file("http://api.wolframalpha.com/v2/query?input=bush person&appid=XXXX");

print_r($mysongs->xpath('/queryresult/pod[@title="Notable facts"]/subpod/plaintext'));

When I try using print_r (as illustrated in the code above), the output is is Array ( [0] => SimpleXMLElement Object ( ) ), rather than the node I am trying to find with the title attribute of "Notable facts".

And here is the XML file I am working with (output from WolframAlpha API):

<queryresult success="true" error="false" numpods="12" datatypes="Book,City,Country,Leader,Movie,People,USState,WikipediaStats" timedout="" timedoutpods="" timing="5.372" parsetiming="0.313" parsetimedout="false" recalculate="" id="MSPa181e9eb33a21fc178e000042dcbd06e674h57h" host="http://www3.wolframalpha.com" server="32" related="http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa191e9eb33a21fc178e000038e39ci99cf7e0g5&s=32" version="2.6">
<pod title="Input interpretation" scanner="Identity" id="Input" position="100" error="false" numsubpods="1">
<subpod title="">
<plaintext>George W. Bush (politician)</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP201e9eb33a21fc178e00004h535c45dfbbb3d1?MSPStoreType=image/gif&s=32" alt="George W. Bush (politician)" title="George W. Bush (politician)" width="185" height="18"/>
</subpod>
</pod>
<pod title="Basic information" scanner="Data" id="BasicInformation:PeopleData" position="200" error="false" numsubpods="1">
<subpod title="">
<plaintext>
full name | George W. Bush date of birth | Saturday, July 6, 1946 (age: 68 years) place of birth | New Haven, Connecticut
</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP211e9eb33a21fc178e00004i6eb4f5hd067h2c?MSPStoreType=image/gif&s=32" alt="full name | George W. Bush date of birth | Saturday, July 6, 1946 (age: 68 years) place of birth | New Haven, Connecticut" title="full name | George W. Bush date of birth | Saturday, July 6, 1946 (age: 68 years) place of birth | New Haven, Connecticut" width="387" height="100"/>
</subpod>
</pod>
<pod title="Image" scanner="Data" id="Image:PeopleData" position="300" error="false" numsubpods="1">
<subpod title="">
<plaintext/>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP221e9eb33a21fc178e00004299did490395cdi?MSPStoreType=image/gif&s=32" alt="" title="" width="113" height="150"/>
</subpod>
</pod>
<pod title="Leadership positions" scanner="Data" id="Position:LeaderData" position="400" error="false" numsubpods="1">
<subpod title="">
<plaintext>
official position | President (43rd) | Governor (Texas) country | United States | United States political affiliation | Republican | Republican start date | January 20, 2001 | January 17, 1995 end date | January 20, 2009 | December 21, 2000
</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP231e9eb33a21fc178e00003bh56cfgd7fahgef?MSPStoreType=image/gif&s=32" alt="official position | President (43rd) | Governor (Texas) country | United States | United States political affiliation | Republican | Republican start date | January 20, 2001 | January 17, 1995 end date | January 20, 2009 | December 21, 2000" title="official position | President (43rd) | Governor (Texas) country | United States | United States political affiliation | Republican | Republican start date | January 20, 2001 | January 17, 1995 end date | January 20, 2009 | December 21, 2000" width="431" height="164"/>
</subpod>
</pod>
<pod title="Timeline" scanner="Data" id="Timeline:PeopleData" position="500" error="false" numsubpods="1">
<subpod title="">
<plaintext/>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP241e9eb33a21fc178e00003d00d07cgi3cgdeb?MSPStoreType=image/gif&s=32" alt="" title="" width="500" height="30"/>
</subpod>
</pod>
<pod title="Notable facts" scanner="Data" id="NotableFacts:PeopleData" position="600" error="false" numsubpods="1">
<subpod title="">
<plaintext>
President of the United States who ordered the invasions of Afghanistan and Iraq following the September 11 attacks Enacted sweeping tax cuts and signed the No Child Left Behind Act Oversaw the creation of the Department of Homeland Security and the controversial USA PATRIOT Act Co-owned the Texas Rangers of Major League Baseball before serving as governor of Texas Son of 41st president of the United States, George H. W. Bush Graduate of Yale University and Harvard School of Business
</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP251e9eb33a21fc178e0000457egg6bfifc8h0b?MSPStoreType=image/gif&s=32" alt="President of the United States who ordered the invasions of Afghanistan and Iraq following the September 11 attacks Enacted sweeping tax cuts and signed the No Child Left Behind Act Oversaw the creation of the Department of Homeland Security and the controversial USA PATRIOT Act Co-owned the Texas Rangers of Major League Baseball before serving as governor of Texas Son of 41st president of the United States, George H. W. Bush Graduate of Yale University and Harvard School of Business" title="President of the United States who ordered the invasions of Afghanistan and Iraq following the September 11 attacks Enacted sweeping tax cuts and signed the No Child Left Behind Act Oversaw the creation of the Department of Homeland Security and the controversial USA PATRIOT Act Co-owned the Texas Rangers of Major League Baseball before serving as governor of Texas Son of 41st president of the United States, George H. W. Bush Graduate of Yale University and Harvard School of Business" width="480" height="227"/>
</subpod>
</pod>
<pod title="Physical characteristics" scanner="Data" id="PhysicalCharacteristics:PeopleData" position="700" error="false" numsubpods="1">
<subpod title="">
<plaintext>height | 6 feet weight | 190 lb (pounds)</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP261e9eb33a21fc178e00000hdd5945h1ch7fb7?MSPStoreType=image/gif&s=32" alt="height | 6 feet weight | 190 lb (pounds)" title="height | 6 feet weight | 190 lb (pounds)" width="193" height="68"/>
</subpod>
<states count="1">
<state name="Metric" input="PhysicalCharacteristics:PeopleData__Metric"/>
</states>
</pod>
<pod title="Familial relationships" scanner="Data" id="FamilialRelationships:PeopleData" position="800" error="false" numsubpods="4">
<subpod title="Parents">
<plaintext>George H.W. Bush | Barbara Bush</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP271e9eb33a21fc178e000062iii38560c3b8a1?MSPStoreType=image/gif&s=32" alt="George H.W. Bush | Barbara Bush" title="George H.W. Bush | Barbara Bush" width="236" height="18"/>
</subpod>
<subpod title="Siblings">
<plaintext>
Jeb Bush | Doro Bush Koch | Robin Bush | Marvin Bush | Neil Bush
</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP281e9eb33a21fc178e00002318g3b4dc6age7h?MSPStoreType=image/gif&s=32" alt="Jeb Bush | Doro Bush Koch | Robin Bush | Marvin Bush | Neil Bush" title="Jeb Bush | Doro Bush Koch | Robin Bush | Marvin Bush | Neil Bush" width="494" height="18"/>
</subpod>
<subpod title="Spouse">
<plaintext>Laura Bush (1977-)</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP291e9eb33a21fc178e00005b3bbd2daafbh061?MSPStoreType=image/gif&s=32" alt="Laura Bush (1977-)" title="Laura Bush (1977-)" width="126" height="18"/>
</subpod>
<subpod title="Children">
<plaintext>Barbara Pierce Bush | Jenna Hager</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP301e9eb33a21fc178e0000605afb6c535d12ia?MSPStoreType=image/gif&s=32" alt="Barbara Pierce Bush | Jenna Hager" title="Barbara Pierce Bush | Jenna Hager" width="243" height="18"/>
</subpod>
<states count="1">
<state name="Show full dates" input="FamilialRelationships:PeopleData__Show full dates"/>
</states>
</pod>
<pod title="Notable films" scanner="Data" id="NotableFilms:PeopleData" position="900" error="false" numsubpods="1">
<subpod title="Appeared in">
<plaintext>
Live Free or Die Hard (2007) | Killing Them Softly (2012) | Definitely, Maybe (2008) | An Inconvenient Truth (2006) | Green Zone (2010) | ... (total: 37)
</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP311e9eb33a21fc178e00006ahe147371i4a2c0?MSPStoreType=image/gif&s=32" alt="Live Free or Die Hard (2007) | Killing Them Softly (2012) | Definitely, Maybe (2008) | An Inconvenient Truth (2006) | Green Zone (2010) | ... (total: 37)" title="Live Free or Die Hard (2007) | Killing Them Softly (2012) | Definitely, Maybe (2008) | An Inconvenient Truth (2006) | Green Zone (2010) | ... (total: 37)" width="482" height="65"/>
</subpod>
<states count="1">
<state name="More" input="NotableFilms:PeopleData__More"/>
</states>
<definitions count="1">
<definition word="Appeared in" desc="Includes films where an individual appeared as him or herself, or in historical or archival footage."/>
</definitions>
</pod>
<pod title="Notable books" scanner="Data" id="NotableBooks:PeopleData" position="1000" error="false" numsubpods="1">
<subpod title="">
<plaintext>Decision Points (2010)</plaintext>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP321e9eb33a21fc178e0000263970449i8e918i?MSPStoreType=image/gif&s=32" alt="Decision Points (2010)" title="Decision Points (2010)" width="143" height="18"/>
</subpod>
</pod>
<pod title="Wikipedia summary" scanner="Data" id="WikipediaSummary:PeopleData" position="1100" error="false" numsubpods="1">
<subpod title="">
<plaintext/>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP331e9eb33a21fc178e000046f898e7ec3088h6?MSPStoreType=image/gif&s=32" alt="" title="" width="473" height="96"/>
</subpod>
<states count="1">
<state name="More" input="WikipediaSummary:PeopleData__More"/>
</states>
<infos count="1">
<info>
<link url="http://en.wikipedia.org/wiki?curid=3414021" text="Full entry"/>
</info>
</infos>
</pod>
<pod title="Wikipedia page hits history" scanner="Data" id="PopularityPod:WikipediaStatsData" position="1200" error="false" numsubpods="1">
<subpod title="">
<plaintext/>
<img src="http://www3.wolframalpha.com/Calculate/MSP/MSP341e9eb33a21fc178e00001b668gaeg4095ih1?MSPStoreType=image/gif&s=32" alt="" title="" width="496" height="154"/>
</subpod>
<states count="1">
<state name="Log scale" input="PopularityPod:WikipediaStatsData__Log scale"/>
</states>
</pod>
<assumptions count="1">
<assumption type="SubCategory" word="bush person" template="Assuming ${desc1}. Use ${desc2} instead" count="2">
<value name="GeorgeWBush::xs353" desc="George W. Bush" input="*DPClash.PersonE.bush+person-_*GeorgeWBush%3A%3Axs353-"/>
<value name="GeorgeHWBush::2r7m6" desc="George H.W. Bush" input="*DPClash.PersonE.bush+person-_*GeorgeHWBush%3A%3A2r7m6-"/>
</assumption>
</assumptions>
<sources count="4">
<source url="http://www.wolframalpha.com/sources/BookDataSourceInformationNotes.html" text="Book data"/>
<source url="http://www.wolframalpha.com/sources/LeaderDataSourceInformationNotes.html" text="Leader data"/>
<source url="http://www.wolframalpha.com/sources/MovieDataSourceInformationNotes.html" text="Movie data"/>
<source url="http://www.wolframalpha.com/sources/PeopleDataSourceInformationNotes.html" text="People data"/>
</sources>
</queryresult>

By all means suggest a better way of working with the output if there is one.

1 Answer 1

1

Did you try this?

$mysongs = simplexml_load_file("http://api.wolframalpha.com/v2/query?input=bush person&appid=$appId");

foreach ($mysongs->xpath('/queryresult/pod[@title="Notable facts"]/subpod/plaintext') as $node) {
    echo (string) $node;
}

//or 

$nodeList = $mysongs->xpath('/queryresult/pod[@title="Notable facts"]/subpod/plaintext');
echo (string) reset($nodeList);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks man! I didn't try that because I thought that the nodelist would be at least partially included in print_r... do you know why it wasn't outputted?
Because print_r gives info about the object instead of casting it to string.
Thanks! I'm learning something new every day. I thought PHP was loosely typed.
Tbh SimpleXMLElement is a strange object which I always manually cast to string if I want the text value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.