I am using the boardgamegeek.com XML API to get the information on a game. I am able to use cURL to retrieve this, but now I have the XML data I don't seem to be able to do anything with it.
This is my cURL code:
<?php
header('Content-type: text/xml');
$id = 226910;
$exp = 1;
$url = 'https://www.boardgamegeek.com/xmlapi/' . (!$exp ? 'boardgame' : 'boardgameexpansion') . '/' . $id . '/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$xml = curl_exec($ch);
if ($xml == false)
{
echo curl_error($ch);
}
curl_close($ch);
And this is my generated output:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<boardgames termsofuse="http://boardgamegeek.com/xmlapi/termsofuse">
<boardgame objectid="226910">
<yearpublished>2017</yearpublished>
<minplayers>1</minplayers>
<maxplayers>5</maxplayers>
<playingtime>0</playingtime>
<minplaytime>0</minplaytime>
<maxplaytime>0</maxplaytime>
<age>14</age>
<name primary="true" sortindex="1">Star Trek: Ascendancy – Borg Assimilation</name>
<description>
This page does not exist. You can edit this page to create it.
</description>
<thumbnail>
https://cf.geekdo-images.com/images/pic3690765_t.jpg
</thumbnail>
<image>https://cf.geekdo-images.com/images/pic3690765.jpg</image>
<boardgamefamily objectid="12210">4X games</boardgamefamily>
<boardgamemechanic objectid="2001">Action Point Allowance System</boardgamemechanic>
<boardgamecategory objectid="1015">Civilization</boardgamecategory>
<boardgamemechanic objectid="2072">Dice Rolling</boardgamemechanic>
<boardgamedesigner objectid="62674">Aaron Dill</boardgamedesigner>
<boardgameartist objectid="76681">Katie Dillon</boardgameartist>
<boardgameversion objectid="354684">English first edition</boardgameversion>
<boardgamecategory objectid="1020">Exploration</boardgamecategory>
<boardgamepublisher objectid="11420">Gale Force Nine, LLC</boardgamepublisher>
<boardgamedesigner objectid="62672">John Kovaleski</boardgamedesigner>
<boardgamemechanic objectid="2011">Modular Board</boardgamemechanic>
<boardgamecategory objectid="1064">Movies / TV / Radio theme</boardgamecategory>
<boardgamecategory objectid="1026">Negotiation</boardgamecategory>
<boardgamecategory objectid="1016">Science Fiction</boardgamecategory>
<boardgamecategory objectid="1113">Space Exploration</boardgamecategory>
<boardgamefamily objectid="7392">Star Trek</boardgamefamily>
<boardgamedesigner objectid="62673">Sean Sweigart</boardgamedesigner>
<boardgamecategory objectid="1086">Territory Building</boardgamecategory>
<boardgamemechanic objectid="2008">Trading</boardgamemechanic>
<boardgamemechanic objectid="2079">Variable Phase Order</boardgamemechanic>
<boardgamemechanic objectid="2015">Variable Player Powers</boardgamemechanic>
<boardgamecategory objectid="1019">Wargame</boardgamecategory>
<boardgamepodcastepisode objectid="213535">What Did You Play This Week Podcast Week 145</boardgamepodcastepisode>
<boardgameartist objectid="62716">Charles Woods</boardgameartist>
<boardgameexpansion objectid="193949" inbound="true">Star Trek: Ascendancy</boardgameexpansion>
<poll name="suggested_numplayers" title="User Suggested Number of Players" totalvotes="1">
<results numplayers="1">
<result value="Best" numvotes="1"/>
<result value="Recommended" numvotes="0"/>
<result value="Not Recommended" numvotes="0"/>
</results>
<results numplayers="2">
<result value="Best" numvotes="1"/>
<result value="Recommended" numvotes="0"/>
<result value="Not Recommended" numvotes="0"/>
</results>
<results numplayers="3">
<result value="Best" numvotes="1"/>
<result value="Recommended" numvotes="0"/>
<result value="Not Recommended" numvotes="0"/>
</results>
<results numplayers="4">
<result value="Best" numvotes="1"/>
<result value="Recommended" numvotes="0"/>
<result value="Not Recommended" numvotes="0"/>
</results>
<results numplayers="5">
<result value="Best" numvotes="0"/>
<result value="Recommended" numvotes="0"/>
<result value="Not Recommended" numvotes="0"/>
</results>
<results numplayers="5+">
<result value="Best" numvotes="0"/>
<result value="Recommended" numvotes="0"/>
<result value="Not Recommended" numvotes="0"/>
</results>
</poll>
<poll name="language_dependence" title="Language Dependence" totalvotes="0">
<results>
<result level="1" value="No necessary in-game text" numvotes="0"/>
<result level="2" value="Some necessary text - easily memorized or small crib sheet" numvotes="0"/>
<result level="3" value="Moderate in-game text - needs crib sheet or paste ups" numvotes="0"/>
<result level="4" value="Extensive use of text - massive conversion needed to be playable" numvotes="0"/>
<result level="5" value="Unplayable in another language" numvotes="0"/>
</results>
</poll>
<poll name="suggested_playerage" title="User Suggested Player Age" totalvotes="1">
<results>
<result value="2" numvotes="0"/>
<result value="3" numvotes="0"/>
<result value="4" numvotes="0"/>
<result value="5" numvotes="0"/>
<result value="6" numvotes="0"/>
<result value="8" numvotes="0"/>
<result value="10" numvotes="0"/>
<result value="12" numvotes="0"/>
<result value="14" numvotes="1"/>
<result value="16" numvotes="0"/>
<result value="18" numvotes="0"/>
<result value="21 and up" numvotes="0"/>
</results>
</poll>
</boardgame>
</boardgames>
I have tried changing my code to the following to try and generate a JSON array:
<?php
// header('Content-type: text/xml');
$id = 226910;
$exp = 1;
$url = 'https://www.boardgamegeek.com/xmlapi/' . (!$exp ? 'boardgame' : 'boardgameexpansion') . '/' . $id . '/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
if ($response == false)
{
echo curl_error($ch);
}
curl_close($ch);
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_decode($json, true);
var_dump($array);
But this just throws the following error:
2017 1 5 0 0 0 14 Star Trek: Ascendancy – Borg Assimilation This page does not exist. You can edit this page to create it. https://cf.geekdo-images.com/images/pic3690765_t.jpg https://cf.geekdo-images.com/images/pic3690765.jpg 4X games Action Point Allowance System Civilization Dice Rolling Aaron Dill Katie Dillon English first edition Exploration Gale Force Nine, LLC John Kovaleski Modular Board Movies / TV / Radio theme Negotiation Science Fiction Space Exploration Star Trek Sean Sweigart Territory Building Trading Variable Phase Order Variable Player Powers Wargame What Did You Play This Week Podcast Week 145 Charles Woods Star Trek: Ascendancy Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/matthew1/public_html/api/index.php on line 19
Warning: simplexml_load_string(): 1 in /home/matthew1/public_html/api/index.php on line 19
Warning: simplexml_load_string(): ^ in /home/matthew1/public_html/api/index.php on line 19 bool(false)
(Note the XML data being displayed without <tags>
before the error message starts.)