Priya X. Pramesi
Priya X. Pramesi

Reputation: 31

eBay's finding API with PHP and SOAP itemFilter not working

I wrote a simple PHP script that retrieves an item from eBay. I'm using PHP and SOAP. I can get the items with other calls just fine. However, I need to use the findItemsAdvanced call to get items from a seller, but I haven't been able to make it work.

Here's what I have so far:

function call_ebay_id_api($itemID){

$location = 'http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=****8&OPERATION-NAME=findItemsAdvanced&itemFilter(0).name=Seller&itemFilter(0).value=****';
$clientebay = new SoapClient(
    "http://developer.ebay.com/webservices/Finding/latest/FindingService.wsdl",
    array(
        "exceptions" => 0,
        "trace" => 1,
        "location"=> $location
        )
);
$requestebay = array("itemFilter" => array("name" => "Seller", "value" => "****"));

$xmlresultsebay = $clientebay->__soapCall('findItemsAdvanced', array($requestebay));
echo "REQUEST HEADERS:\n<pre>" . htmlentities($clientebay->__getLastRequestHeaders()) . "</pre>\n";
echo "REQUEST:\n<pre>" . htmlentities($clientebay->__getLastRequest()) . "</pre>\n";
echo "RESPONSE:\n<pre>" . htmlentities($clientebay->__getLastResponse()) . "</pre>\n";
echo 'd';
return $xmlresultsebay;

and the XML request from this is:

<?xml version="1.0" encoding="UTF-8"?>
  <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.ebay.com/marketplace/search/v1/services">
     <SOAP-ENV:Body>
         <ns1:findItemsAdvancedRequest>
             <itemFilter>
                 <name>Seller</name>
                 <value>*seller name here*</value>
              </itemFilter>
          </ns1:findItemsAdvancedRequest>
     </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

which looks like the example they have on the API's page

However, the response that I got back is an error. It says I need to specify keywords or category ID. When I do that, I get success, but the itemFilter isn't applied.

Can anyone help me here?

Upvotes: 2

Views: 868

Answers (2)

deadcow
deadcow

Reputation: 1

Instead of building a Soapvar for every variable create a subclass of \SoapClient

namespace whatever;
class EbaySoapClient extends \SoapClient{
    public function __doRequest($request, $location, $action, $version, $one_way = 0) {
        $request = str_replace(['ns1:', ':ns1'], '', $request);
        $doreq = parent::__doRequest($request, $location, $action, $version, $one_way);
        $this->__last_request = $request;
        return $doreq;
    }
}

example call:

$wsdl = "http://developer.ebay.com/webservices/Finding/latest/FindingService.wsdl";
$endpointprod = "http://svcs.ebay.com/services/search/FindingService/v1";
$endpointbox ="http://svcs.sandbox.ebay.com/services/search/FindingService/v1";
$sandboxApi = "YourSandboxApiKey";
$prodApi = "YourProdApiKey";

$headers = stream_context_create(['http' =>
    [   
        'method' => 'POST',
        'header' => implode(PHP_EOL, [
            'X-EBAY-SOA-SERVICE-NAME: FindingService',
            'X-EBAY-SOA-OPERATION-NAME: findCompletedItems',
            'X-EBAY-SOA-SERVICE-VERSION: 1.0.0',
            'X-EBAY-SOA-GLOBAL-ID: EBAY-US',
            'X-EBAY-SOA-SECURITY-APPNAME: ' . $sandboxApi,
            'X-EBAY-SOA-REQUEST-DATA-FORMAT: XML',
            'X-EBAY-SOA-MESSAGE-PROTOCOL: SOAP12',
            'Content-Type: text/xml;charset=utf-8',])
        ]
    ]
);
$options = [
    'trace' => true,
    'cache' => WSDL_CACHE_NONE,
    'exceptions' => true,
    'soap_version' => SOAP_1_2,
    'location' => $endpointbox,
    'stream_context' => $headers
];
$soap = new \whatever\EbaySoapClient($wsdl, $options);
$array = [
    'keywords' => 'clock', 
    'itemFilter' => [
        ['name' => 'Condition', 'value' => 3000],
        ['name' => 'FreeShippingOnly', 'value' => 1]
     ]

];
$response = $soap->findCompletedItems($array);
echo $soap->__getLastRequest();
var_dump($response);

not the most eloquent but works for me, you can scrap the namespace if you want just put it up there for illustrative purposes. It sets the default namespace.

Upvotes: 1

Priya X. Pramesi
Priya X. Pramesi

Reputation: 31

Mkay so I got it working...I guess?

So I thought the problem was that eBay's itemFilter requires an ItemFilter type (notice the capital I in the ItemFilter, btw), instead of an array. This led me to create classes with the same names as the types required for the request call. So I created objects called ItemFilter, Name, Value, etc. However, it still didn't work.

I almost gave up and start over maybe with cURL or something...or even Cajun voodoo since nothing else worked, so hey, why not magic?

After I went to the cemetery of Madam Marie Laveau with a piece of alligator skin and some boudin links to put on her grave, I got really hungry and started thinking about the boudin. "Hmm...delicious rice and pig offal inside the pig intestine wrapper...hold on," I thought to myself "wrapper? I should check if the variable is wrapped correctly." So after I got home (with some more boudin from the local market), I checked the wrapper and it was correct. But then I noticed that the namespace for the itemFilter was missing. So I started messing around with PHP SoapVar...and it worked! "Finally, a dim spark in this god forsaken web-dev abyss," so I thought.

So basically instead of:

$requestebay = array("itemFilter" => array("name" => "Seller", "value" => "****"));

I should've put:

$itemFilters = array(
    "name" => new SoapVar("Seller", XSD_STRING, NULL, NULL, NULL, 'http://www.ebay.com/marketplace/search/v1/services'),
    "value" => new SoapVar("****", XSD_STRING, NULL, NULL, NULL, 'http://www.ebay.com/marketplace/search/v1/services')
);
$requestebay = array(
    "itemFilter" => new SoapVar($itemFilters, SOAP_ENC_OBJECT, NULL, NULL, NULL, 'http://www.ebay.com/marketplace/search/v1/services')
);

...and BAM! that solved my problem. I guess the lesson to be learned here is that voodoo is a whole lot better than eBay's documentation, since with voodoo I don't have to dig very deep to find things for my next project.

Upvotes: 1

Related Questions