dsfbnhc4373 2013-03-25 16:37
浏览 72
已采纳

使用eBay API在PHP中将XML显示为响应

Following tutorial shows how to display data as HTML based on that I just need to show response of XML as opposed to parsing it. I have tried to find solution but didn't have any luck. Any help will be appreciated.

<?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\"?>
";
$xmlrequest .= "<findItemsByKeywordsRequest                  
xmlns=\"http://www.ebay.com/marketplace/search/v1/services\">
";
$xmlrequest .= "<keywords>";
$xmlrequest .= $query;
$xmlrequest .= "</keywords>
";
$xmlrequest .= "<paginationInput>
 
<entriesPerPage>3</entriesPerPage>
</paginationInput>
";
$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条回答 默认 最新

  • duanpiyao2734 2013-03-25 20:41
    关注

    Try changing this (wrapped for clarity):

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

    to this:

    $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
    

    The original feeds the XML response immediately into an XML parser, and creates an XML object with it (look up SimpleXML in the PHP manual for details). Here, we've split the two calls up, so the XML string is now saved in an intermediate variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?