skip to Main Content

I’d like to make a soap call to the eBay api and finds products by keywords. With the help of eBay documentation and other online resources, I came up with this code:

$client = new SoapClient('http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl');
$soap_headers = array(
    new SoapHeader('X-EBAY-SOA-OPERATION-NAME', 'findItemsByKeywords'),
    new SoapHeader('X-EBAY-SOA-SERVICE-VERSION', '1.3.0'),
    new SoapHeader('X-EBAY-SOA-REQUEST-DATA-FORMAT', 'XML'),
    new SoapHeader('X-EBAY-SOA-GLOBAL-ID', 'EBAY-US'),
    new SoapHeader('X-EBAY-SOA-SECURITY-APPNAME', '<there would be the key>'),
);
$client->__setSoapHeaders($soap_headers);

// Call wsdl function
$result = $client->__soapCall("findItemsByKeywords", array("keywords" => "Potter"));

However, this code results in an error:
“Service operation is unknown,
500 Internal Server Error – SoapFault”

I tried changing the first line into this (don’t know why it should make a difference, but I saw it somewhere):

$client = new SoapClient(NULL, array(
"location" => 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1', 
'uri' => 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1')
);

And now this result in this error: Missing SOA operation name header,
500 Internal Server Error – SoapFault

Does anybody know what causes these errors to occur and how to fix them?

Thank you, Mike!

2

Answers


  1. The thing is, the service expects HTTP Headers (or query string parameters, apparently, by reading their guide a bit).

    With __setSoapHeaders you are, well, setting SOAP Headers.

    Try this, for instance

    $wsdl = 'http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl';
    
    $appID = 'YOUR KEY/ID';
    
    $headers = implode(PHP_EOL, [
      "X-EBAY-SOA-OPERATION-NAME: findItemsByKeywords",
      "X-EBAY-SOA-SECURITY-APPNAME: $appID"
    ]);
    
    $options = [
      'trace' => true,
      'cache' => WSDL_CACHE_NONE,
      'exceptions' => true,
      'stream_context' => stream_context_create([
        'http' => [
          'header' => $headers
        ]
      ]),
    ];
    
    $client = new SoapClient($wsdl, $options);
    
    $response = $client->findItemsByKeywords([
      'keywords' => 'Potter'
    ]);
    
    Login or Signup to reply.
  2. The following code works.

    define('APP_ID', '*** Your App ID ***');
    $wsdl = 'http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl';
    // For sandbox
    $endpoint_uri = 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1';
    // For production
    //$endpoint_uri = 'http://svcs.ebay.com/services/search/FindingService/v1';
    
    // We'll use this namespace explicitly for the 'keywords' tag,
    // because the SoapClient doesn't apply it automatically.
    $ns = 'http://www.ebay.com/marketplace/search/v1/services';
    // The SOAP function
    $operation = 'findItemsByKeywords';
    
    $http_headers = implode(PHP_EOL, [
      "X-EBAY-SOA-OPERATION-NAME: $operation",
      "X-EBAY-SOA-SECURITY-APPNAME: " . APP_ID,
    ]);
    
    $options = [
      'trace' => true,
      'cache' => WSDL_CACHE_NONE,
      'exceptions' => true,
      'location' => $endpoint_uri,
      //'uri' => 'ns1',
      'stream_context' => stream_context_create([
        'http' => [
          'method' => 'POST',
          'header' => $http_headers,
        ]
      ]),
    ];
    
    $client = new SoapClient($wsdl, $options);
    
    try {
      $wrapper = new StdClass;
      $wrapper->keywords = new SoapVar('harry potter', XSD_STRING,
        null, null, null, $ns);
    
      $result = $client->$operation(new SoapVar($wrapper, SOAP_ENC_OBJECT));
      var_dump($result);
    } catch (Exception $e) {
      echo $e->getMessage();
    }
    

    As already mentioned, one of the issues is that you pass X-EBAY-SOA-* values as SOAP headers. The service expects them as HTTP headers:

    HTTP Headers or URL Parameters—Each Finding API call requires certain
    HTTP headers or URL parameters. For example, you must specify your
    AppID in the X-EBAY-SOA-SECURITY-APPNAME header or SECURITY-APPNAME
    URL parameter. Similarly, you must always specify the call name in the
    X-EBAY-SOA-OPERATION-NAME header or OPERATION-NAME URL parameter.
    Other headers are optional or conditionally required.

    The second issue is that the SoapClient location option is not specified. It should contain an URI to one of the service endpoints. Otherwise, the API returns Service operation is unknown error.

    Finally, the SoapClient doesn’t put the keywords into Ebay’s namespace:

    <?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:findItemsByKeywordsRequest>
          <keywords>harry potter</keywords>
        </ns1:findItemsByKeywordsRequest>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    As a result, the API returns an error: Keywords value required.. So I’ve specified the namespace for the keywords tag explicitly.

    Also note the use of different endpoint URIs for sandbox and for production.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search