ebay-sdk-php通知处理

jda*_*awg 0 php ebay-api ebay-sdk

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

这对社区来说是一个很好的贡献,尽管我已经成功完成了大部分工作.

我一直在圈子里发布通知,特别是'FixedPriceTransaction'通知.我已设法订阅通知并发送请求以确保已正确创建订阅.

不幸的是,当eBay发送通知来处理它时,我无法确定使用哪种方法.谁能摆脱任何光明?

Dav*_*ler 6

完全披露:我是eBay SDK的开发者

eBay的通知服务不是SDK正式支持的,因为它是API的一个我不熟悉的领域,但我会尽力回答你的问题.

据我了解,eBay的平台通知分为两个部分.

  1. 您通知易趣您感兴趣的用户通知.
  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') {

}
Run Code Online (Sandbox Code Playgroud)

第二部分可能与SDK有关,但这是我没有任何经验的一个领域.据我了解,eBay将向您控制的URL发送POST HTTP请求.您有责任处理请求中包含的数据并使用标准HTTP状态200 OK进行响应.我假设POST数据包含一个SOAP主体,PHP脚本可以将其作为字符串访问.SOAP主体内部应该是通知的XML.只要你有办法获得eBay发送的实际XML字符串,你就可以使用XmlParser类.此类是SDK用于将API响应从API转换回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;
Run Code Online (Sandbox Code Playgroud)