2017-11-22 1 views
0

boardgamegeek.com XML API을 사용하여 게임에 대한 정보를 얻습니다. 이 URL을 검색하는 데 cURL을 사용할 수 있지만 이제는 XML 데이터를 가지고있어 아무 것도 할 수없는 것 같습니다.cURL로 생성 된 XML 파일을 JSON으로 변환

이 내 컬 코드 :

<?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); 

그리고 이것은 내 생성 된 출력입니다 : 내가 JSON 배열을 시도하고 생성하기 위해 다음에 내 코드를 변경

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> 

을 시도 :

<?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); 

하지만 다음과 같은 오류가 발생합니다.

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)

(오류 메시지가 시작되기 전에 XML 데이터를 참고 <tags>없이 표시된다.)

+0

감사의 익명 ... – mpdc

+0

가능한 복제를 [방법 xml을 php의 배열로 변환 하시겠습니까?] (https://stackoverflow.com/questions/6578832/how-to-convert-xml-into-array-in-php) – hanshenrik

답변

1

당신은 옵션 CURLOPT_RETURNTRANSFER 설정하는 것을 잊었다 :이 할당 한 후이 옵션을 사용하지 않으면

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly (source).

$response = curl_exec($ch);이면 $response 변수는 부울 값 true을 포함합니다.

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found

하는 유효한 XML 헤더 사용 보내려면 : 다음 코드 $xml = simplexml_load_string($response);를 실행 한 후 오류 얻을 잘 설명 downvote에 대한

curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: text/xml"]); 
+0

@mpdc,이 문제를 해결하는 데 도움이 되었습니까? – camelsWriteInCamelCase

관련 문제