【问题标题】:ebay-sdk-php notification handlingebay-sdk-php 通知处理
【发布时间】:2016-03-15 17:22:29
【问题描述】:

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

我一直在讨论通知,尤其是“FixedPriceTransaction”通知。我已成功订阅通知并发送请求以确保订阅已正确创建。

很遗憾,当 eBay 发送通知进行处理时,我无法确定使用哪种方法。

【问题讨论】:

    标签: php ebay-api ebay-sdk


    【解决方案1】:

    全面披露:我是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 类。 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;
    

    【讨论】:

    • 太棒了!第一部分我已经有了类似的东西,但第二部分非常有用。非常感谢!
    • 谢谢。你能告诉我是否有办法知道 ebay 是否向 url 发送了通知。我正在添加日志消息但没有被调用。电子邮件开发网址有效
    • 我正准备在github上提交和发布,但这完全回答了我的问题!
    • 你好@coder771 你有这个通知处理程序可用吗?我被困在这两天了,提前谢谢
    • 你好@nikksan 有任何改变,你有这个通知处理程序可用吗?我被困在这两天了,提前谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多