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