从ebay api获取xml响应

时间:2014-04-26 11:18:29

标签: php xml ebay

我已经创建了代码来获取ebay的响应。我在ebay中使用沙箱测试,这给出了正确的结果。

但是在localhost示例中它没有给出响应:

以下代码:

$xmlResponse = simplexml_load_string($response);
var_dump($xmlResponse);

它返回:false;

知道这里有什么问题吗?

代码:

require_once 'tradingConstants.php';
//DEFINE("API_URL",'https://api.sandbox.ebay.com/ws/api.dll');



// check if posted

    // grab our posted keywords and call helper function
    // TODO: check if need urlencode
    $title = $_POST['title'];
    $categoryID = $_POST['categoryID'];
    $startPrice = $_POST['startPrice'];
    $pictureURL = $_POST['pictureURL'];
    $description = $_POST['description'];

    // call the getAddItem function to make AddItem call
  $response = getAddItem($title, $categoryID, $startPrice, $pictureURL, $description);
// Function to call the Trading API AddItem

function getAddItem($addTitle, $addCatID, $addSPrice, $addPicture, $addDesc) {

    /* Sample XML Request Block for minimum AddItem request
    see ... for sample XML block given length*/

    // Create unique id for adding item to prevent duplicate adds
    $uuid = md5(uniqid());

    // create the XML request
    $xmlRequest  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    $xmlRequest .= "<AddItemRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">";
    $xmlRequest .= "<ErrorLanguage>en_US</ErrorLanguage>";
  $xmlRequest .= "<WarningLevel>High</WarningLevel>";
  $xmlRequest .= "<Item>";
  $xmlRequest .= "<Title>" . $addTitle . "</Title>";
  $xmlRequest .= "<Description>" . $addDesc . "</Description>";
  $xmlRequest .= "<PrimaryCategory>";
  $xmlRequest .= "<CategoryID>" . $addCatID . "</CategoryID>";
  $xmlRequest .= "</PrimaryCategory>";
  $xmlRequest .= "<StartPrice>" . $addSPrice . "</StartPrice>";
  $xmlRequest .= "<ConditionID>1000</ConditionID>";
  $xmlRequest .= "<CategoryMappingAllowed>true</CategoryMappingAllowed>";
  $xmlRequest .= "<Country>US</Country>";
  $xmlRequest .= "<Currency>USD</Currency>";
  $xmlRequest .= "<DispatchTimeMax>3</DispatchTimeMax>";
  $xmlRequest .= "<ListingDuration>Days_7</ListingDuration>";
  $xmlRequest .= "<ListingType>Chinese</ListingType>";
  $xmlRequest .= "<PaymentMethods>PayPal</PaymentMethods>";
  $xmlRequest .= "<PayPalEmailAddress>yourpaypal@emailaddress.com</PayPalEmailAddress>";
  $xmlRequest .= "<PictureDetails>";
  $xmlRequest .= "<PictureURL>" . $addPicture . "</PictureURL>";
  $xmlRequest .= "</PictureDetails>";
  $xmlRequest .= "<PostalCode>05485</PostalCode>";
  $xmlRequest .= "<Quantity>1</Quantity>";
  $xmlRequest .= "<ReturnPolicy>";
  $xmlRequest .= "<ReturnsAcceptedOption>ReturnsAccepted</ReturnsAcceptedOption>";
  $xmlRequest .= "<RefundOption>MoneyBack</RefundOption>";
  $xmlRequest .= "<ReturnsWithinOption>Days_30</ReturnsWithinOption>";
  $xmlRequest .= "<Description>" . $addDesc . "</Description>";
  $xmlRequest .= "<ShippingCostPaidByOption>Buyer</ShippingCostPaidByOption>";
  $xmlRequest .= "</ReturnPolicy>";
  $xmlRequest .= "<ShippingDetails>";
  $xmlRequest .= "<ShippingType>Flat</ShippingType>";
  $xmlRequest .= "<ShippingServiceOptions>";
    $xmlRequest .= "<ShippingServicePriority>1</ShippingServicePriority>";
  $xmlRequest .= "<ShippingService>USPSMedia</ShippingService>";
  $xmlRequest .= "<ShippingServiceCost>2.50</ShippingServiceCost>";
  $xmlRequest .= "</ShippingServiceOptions>";
  $xmlRequest .= "</ShippingDetails>";
  $xmlRequest .= "<Site>US</Site>";
  $xmlRequest .= "<UUID>" . $uuid . "</UUID>";
  $xmlRequest .= "</Item>";
  $xmlRequest .= "<RequesterCredentials>";
  $xmlRequest .= "<eBayAuthToken>" . AUTH_TOKEN . "</eBayAuthToken>";
  $xmlRequest .= "</RequesterCredentials>";
  $xmlRequest .= "<WarningLevel>High</WarningLevel>";
    $xmlRequest .= "</AddItemRequest>";

    // define our header array for the Trading API call
    // notice different headers from shopping API and SITE_ID changes to SITEID
    $headers = array(
        /*'X-EBAY-API-SITEID:'.SITEID,
        'X-EBAY-API-CALL-NAME:addItem',
        'X-EBAY-API-REQUEST-ENCODING:XML',
        'X-EBAY-API-COMPATIBILITY-LEVEL:' . API_COMPATIBILITY_LEVEL,
        'X-EBAY-API-DEV-NAME:' . API_DEV_NAME,
        'X-EBAY-API-APP-NAME:' . API_APP_NAME,
        'X-EBAY-API-CERT-NAME:' . API_CERT_NAME,*/

        'X-EBAY-API-APP-ID:Startupb1-efb9-43ca-a0e8-473753fe9ad,
        X-EBAY-API-VERSION:863,
        X-EBAY-API-SITE-ID:203,
        X-EBAY-API-CALL-NAME:AddItem,
        X-EBAY-API-REQUEST-ENCODING:XML,
        Content-Type: text/xml;charset=utf-8'
    );
    $API_URL = 'https://api.sandbox.ebay.com/ws/api.dll';
    // initialize our curl session
    $session  = curl_init();
    curl_setopt($session, CURLOPT_URL, $API_URL);
    // set our curl options with the XML request
    curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($session, CURLOPT_POST, true);
    curl_setopt($session, CURLOPT_POSTFIELDS, $xmlRequest);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

    // execute the curl request
    $responseXML = curl_exec($session);

    // close the curl session
    curl_close($session);
    //echo $addTitle;
    // return the response XML
    return $responseXML;
}

1 个答案:

答案 0 :(得分:1)

simplexml_load_string返回类SimpleXMLElement的对象,其属性包含xml文档中包含的数据,或者 FALSE 失败。

您需要找出getAddItem死亡的原因不会返回有效的xml