2013-03-25 4 views
0

다음은 tutorial을 기반으로 데이터를 HTML로 표시하는 방법을 보여 주며, 구문 분석과 달리 XML의 응답 만 표시하면됩니다. 솔루션을 찾으려고했지만 아무런 운이 없었습니다. 어떤 도움을 주시면 감사하겠습니다.eBay API를 사용하여 PHP에서 XML을 응답으로 표시

<?php 

$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1'; // URL to call 
$query = 'iphone';     // Supply your own query keywords as needed 

// Construct the findItemsByKeywords POST call 
// Load the call and capture the response returned by the eBay API 
// The constructCallAndGetResponse function is defined below 
$resp = simplexml_load_string(constructPostCallAndGetResponse($endpoint, $query)); 

// Check to see if the call was successful, else print an error 
if ($resp->ack == "Success") { 
$results = ''; // Initialize the $results variable 

// Parse the desired information from the response 
foreach($resp->searchResult->item as $item) { 
$link = $item->viewItemURL; 
$title = $item->title; 

// Build the desired HTML code for each searchResult.item node and append it to $results 
$results .= "<tr><td><img src=\"$pic\"></td><td><a href=\"$link\">$title</a></td></tr>"; 
} 
} 
else { // If the response does not indicate 'Success,' print an error 
    $results = "<h3>Oops! The request was not successful"; 
    $results .= "AppID for the Production environment.</h3>"; 
} 

function constructPostCallAndGetResponse($endpoint, $query) { 
global $xmlrequest; 

// Create the XML request to be POSTed 
$xmlrequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; 
$xmlrequest .= "<findItemsByKeywordsRequest     
xmlns=\"http://www.ebay.com/marketplace/search/v1/services\">\n"; 
$xmlrequest .= "<keywords>"; 
$xmlrequest .= $query; 
$xmlrequest .= "</keywords>\n"; 
$xmlrequest .= "<paginationInput>\n 
<entriesPerPage>3</entriesPerPage>\n</paginationInput>\n"; 
$xmlrequest .= "</findItemsByKeywordsRequest>"; 

// Set up the HTTP headers 
$headers = array(
'X-EBAY-SOA-OPERATION-NAME: findItemsByKeywords', 
'X-EBAY-SOA-SERVICE-VERSION: 1.3.0', 
'X-EBAY-SOA-REQUEST-DATA-FORMAT: XML', 
'X-EBAY-SOA-GLOBAL-ID: EBAY-GB', 
'X-EBAY-SOA-SECURITY-APPNAME: ******', 
'Content-Type: text/xml;charset=utf-8', 
); 

$session = curl_init($endpoint);      // create a curl session 
curl_setopt($session, CURLOPT_POST, true);    // POST request type 
curl_setopt($session, CURLOPT_HTTPHEADER, $headers); // set headers using $headers 
array 
curl_setopt($session, CURLOPT_POSTFIELDS, $xmlrequest); // set the body of the POST 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // 

$responsexml = curl_exec($session);      // send the request 
curl_close($session);         // close the session 
return $responsexml;         // returns a string 


} // End of constructPostCallAndGetResponse function 
?> 
+1

지금까지 시도한 것을 볼 수 있습니까? 여기에 제기 된 질문에는 사전에 상당한 노력이나 일부 비공식 코드가 나와야하므로 응답자가 함께 작업 할 항목이 있습니다. 불행히도이 코드는 어느 것도 가지고 있지 않으므로 현재 닫혀있을 가능성이 있습니다. – halfer

+0

튜토리얼에서는 xml을 html로 구문 분석하는 코드를 보여줍니다. 난 그게 XML 노드 트리를 표시하고 싶습니다. – user2208358

+0

확인. 그럼에도 불구하고, 먼저 _try_하시기 바랍니다. 이미 예제가 작동합니까? – halfer

답변

0

시도 변경이 (중략)

$resp = simplexml_load_string(
    constructPostCallAndGetResponse($endpoint, $query) 
); 

이에 :

$xmlString = constructPostCallAndGetResponse($endpoint, $query); 
$resp = simplexml_load_string($xmlString); 
// Now you have your raw XML to play with 
echo htmlentities($xmlString); 
// The XML document $resp is available too, if you wish to do 
// something with that 

을 원래는 XML 파서로 즉시 XML 응답 피드 및 XML 객체를 생성 자세한 내용은 PHP 매뉴얼에서 SimpleXML을 참조하십시오. 여기에서는 두 개의 호출을 분리하여 XML 문자열을 중간 변수에 저장합니다.

+1

그건 내 문제를 해결했습니다. 도움과 인내심에 감사드립니다. – user2208358

+0

반환 된 XML을 서버의 XML 파일에 저장할 수 있습니까? – user2208358

+0

물론,'file_put_contents()'를 사용하십시오. – halfer

관련 문제