ebay-sdk-php 通知处理

ebay-sdk-php notification handling

我正在使用 https://github.com/davidtsadler/ebay-sdk-php,eBay 的非官方 PHP SDK。

我一直在围绕通知转圈,尤其是 'FixedPriceTransaction' 通知。我已成功订阅通知并发送请求以确保已正确创建订阅。

不幸的是,当eBay发送通知来处理它时,我不知道该使用哪种方法。

完全披露:我是 eBay SDK

的开发者

eBay 的通知服务不是 SDK 官方支持的东西,因为它是 API 的一个领域,我不熟悉,但我会尽力回答你的问题可以。

据我所知,eBay 的平台通知有两个部分。

  1. 您告知 eBay 您对哪些用户通知感兴趣。
  2. eBay 的平台通知然后将通知异步推送到您的交付位置(通常是您控制的域下的 URL。)

第一部分应该可以通过 SDK 实现,因为它只涉及向 SetNotificationPreferences 操作发送请求。听起来您已经知道如何完成这部分,但我已经包含了下面的示例,只是希望它能有所帮助。我没有尝试过下面的代码,但它应该可以让您了解该怎么做。

use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;

/**
 * Fill out $request according to your project needs.
 */
$request = new Types\SetNotificationPreferencesRequest();
$request->UserDeliveryPreferenceArray = new Types\NotificationEnableArrayType();
$notification = new Types\NotificationEnableType();
$notification->EventEnable = 'Enable';
$notification->EventType = 'FixedPriceTransaction';
$request->UserDeliveryPreferenceArray->NotificationEnable[] = $notification;

/**
 * Handle response according to your project needs.
 */ 
$response = $service->SetNotificationPreferences($request);
if ($response->Ack !== 'Failure') {

}

第二部分可能可以使用 SDK,但这是我没有任何经验的领域。据我了解,eBay 将向您控制的 URL 发送 POST HTTP 请求。您有责任处理请求中包含的数据并以标准 HTTP 状态 200 OK 进行响应。我假设 POST 数据包含一个 SOAP 主体,PHP 脚本可以将其作为字符串访问。 SOAP 体内应该是通知的 XML。只要您有办法获得 eBay 发送的实际 XML 字符串,就可以使用 XmlParser class。这个 class 是 SDK 用来将 API 的 XML 响应转换回 PHP 对象的东西。这意味着您也可以这样做。

<?php
require __DIR__.'/vendor/autoload.php';

use DTS\eBaySDK\Parser;

/**
 * This string is not a complete example of what eBay could send.
 * A full example can be found at http://developer.ebay.com/Devzone/guides/ebayfeatures/Notifications/Notif-EndOfAuction.html#Example
 * It assumes that eBay sends a POST request to your sever and 
 * that you can obtain the data as a string, E.g via $_POST[] or some other way.
 */
$soap = <<<EOF_S
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header>
    <ebl:RequesterCredentials soapenv:mustUnderstand="0" xmlns:ns="urn:ebay:apis:eBLBaseComponents" xmlns:ebl="urn:ebay:apis:eBLBaseComponents">
      <ebl:NotificationSignature xmlns:ebl="urn:ebay:apis:eBLBaseComponents">1w5Fdyr9V9ofTq67etR0lA==</ebl:NotificationSignature>
    </ebl:RequesterCredentials>
  </soapenv:Header>
    <soapenv:Body>
        <GetItemTransactionsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
            <Item>
                <ItemID>123456789</ItemID>
            </Item>
    </GetItemTransactionsResponse>
    </soapenv:Body>
</soapenv:Envelope>
EOF_S;

/** 
 * Very simple method of extracting the XML from the string.
 */
$matches = array();
preg_match('#<soapenv:Body>(.*?)</soapenv:Body>#s', $soap, $matches);

$xml = $matches[1];

/**
 * The parser requires the full namespace and classname of the object that will be built from the XML.
 */
$parser = new Parser\XmlParser('DTS\eBaySDK\Trading\Types\GetItemTransactionsResponseType');
/** 
 * Pass the XML and the parser will return a PHP object.
 */
$response = $parser->parse($xml);
/**
 * Use the object.
 */
echo $response->Item->ItemID;