0

I've got this application that exchanges data with eBay Trading API using PHP/XML. It's been working fine for 3 weeks but since yesterday I wasn't able to connect anymore.

For simplicity, I've created a very simple script to test the communication, but I'm still stuck with the same problem.

//show all errors - useful whilst developing
error_reporting(E_ALL);

// these keys can be obtained by registering at http://developer.ebay.com

$production = true;   // toggle to true if going against production
$compatabilityLevel = 833;    // eBay API version
$siteID = 3;                            // siteID needed in request - US=0, UK=3, DE=77...

if ($production) {
    $devID = '[devID]';   // these prod keys are different from sandbox keys
    $appID = '[appID]';
    $certID = '[certID]';

    //set the Server to use (Sandbox or Production)
    $serverUrl = 'https://api.ebay.com/ws/api.dll';      // server URL different for prod and sandbox
    //the token representing the eBay user to assign the call with
    $userToken = '[my_token]';
}

class eBaySession {

    private $requestToken;
    private $devID;
    private $appID;
    private $certID;
    private $serverUrl;
    private $compatLevel;
    private $siteID;
    private $verb;

    /**     __construct
      Constructor to make a new instance of eBaySession with the details needed to make a call
      Input:    $userRequestToken - the authentication token fir the user making the call
      $developerID - Developer key obtained when registered at http://developer.ebay.com
      $applicationID - Application key obtained when registered at http://developer.ebay.com
      $certificateID - Certificate key obtained when registered at http://developer.ebay.com
      $useTestServer - Boolean, if true then Sandbox server is used, otherwise production server is used
      $compatabilityLevel - API version this is compatable with
      $siteToUseID - the Id of the eBay site to associate the call iwht (0 = US, 2 = Canada, 3 = UK, ...)
      $callName  - The name of the call being made (e.g. 'GeteBayOfficialTime')
      Output:   Response string returned by the server
     */
    public function __construct($userRequestToken, $developerID, $applicationID, $certificateID, $serverUrl, $compatabilityLevel, $siteToUseID, $callName) {
        $this->requestToken = $userRequestToken;
        $this->devID = $developerID;
        $this->appID = $applicationID;
        $this->certID = $certificateID;
        $this->compatLevel = $compatabilityLevel;
        $this->siteID = $siteToUseID;
        $this->verb = $callName;
        $this->serverUrl = $serverUrl;
    }

    /**     sendHttpRequest
      Sends a HTTP request to the server for this session
      Input:    $requestBody
      Output:   The HTTP Response as a String
     */
    public function sendHttpRequest($requestBody) {
        //build eBay headers using variables passed via constructor
        $headers = $this->buildEbayHeaders();

        //initialise a CURL session
        $connection = curl_init();
        //set the server we are using (could be Sandbox or Production server)
        curl_setopt($connection, CURLOPT_URL, $this->serverUrl);

        //stop CURL from verifying the peer's certificate
        curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);

        curl_setopt($connection, CURLOPT_VERBOSE, TRUE);
        curl_setopt($connection, CURLOPT_TIMEOUT, 30);

        //set the headers using the array of headers
        curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);

        //set method as POST
        curl_setopt($connection, CURLOPT_POST, 1);

        //set the XML body of the request
        curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody);

        //set it to return the transfer as a string from curl_exec
        curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);

        //Send the Request
        $response = curl_exec($connection);

        //close the connection
        curl_close($connection);

        //return the response
        return $response;
    }

    /**     buildEbayHeaders
      Generates an array of string to be used as the headers for the HTTP request to eBay
      Output:   String Array of Headers applicable for this call
     */
    private function buildEbayHeaders() {
        $headers = array(
            //Regulates versioning of the XML interface for the API
            'X-EBAY-API-COMPATIBILITY-LEVEL: ' . $this->compatLevel,
            //set the keys
            'X-EBAY-API-DEV-NAME: ' . $this->devID,
            'X-EBAY-API-APP-NAME: ' . $this->appID,
            'X-EBAY-API-CERT-NAME: ' . $this->certID,
            //the name of the call we are requesting
            'X-EBAY-API-CALL-NAME: ' . $this->verb,
            //SiteID must also be set in the Request's XML
            //SiteID = 0  (US) - UK = 3, Canada = 2, Australia = 15, ....
            //SiteID Indicates the eBay site to associate the call with
            'X-EBAY-API-SITEID: ' . $this->siteID,
        );

        return $headers;
    }

}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
    <HEAD>
        <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <TITLE>GeteBayOfficialTime</TITLE>
    </HEAD>
    <BODY>

<?php
//SiteID must also be set in the Request's XML
//SiteID = 0  (US) - UK = 3, Canada = 2, Australia = 15, ....
//SiteID Indicates the eBay site to associate the call with
$siteID = 3;
//the call being made:
$verb = 'GeteBayOfficialTime';
//Level / amount of data for the call to return (default = 0)
$detailLevel = 0;

///Build the request Xml string
$requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
$requestXmlBody .= '<GeteBayOfficialTimeRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
$requestXmlBody .= "<RequesterCredentials><eBayAuthToken>$userToken</eBayAuthToken></RequesterCredentials>";
$requestXmlBody .= '</GeteBayOfficialTimeRequest>';

//Create a new eBay session with all details pulled in from included keys.php
$session = new eBaySession($userToken, $devID, $appID, $certID, $serverUrl, $compatabilityLevel, $siteID, $verb);
//send the request and get response
$responseXml = $session->sendHttpRequest($requestXmlBody);
if (stristr($responseXml, 'HTTP 404') || $responseXml == '')
    die('<P>Error sending request');

//Xml string is parsed and creates a DOM Document object
$responseDoc = new DomDocument();
$responseDoc->loadXML($responseXml);


//get any error nodes
$errors = $responseDoc->getElementsByTagName('Errors');

//if there are error nodes
if ($errors->length > 0) {
    echo '<P><B>eBay returned the following error(s):</B>';
    //display each error
    //Get error code, ShortMesaage and LongMessage
    $code = $errors->item(0)->getElementsByTagName('ErrorCode');
    $shortMsg = $errors->item(0)->getElementsByTagName('ShortMessage');
    $longMsg = $errors->item(0)->getElementsByTagName('LongMessage');
    //Display code and shortmessage
    echo '<P>', $code->item(0)->nodeValue, ' : ', str_replace(">", "&gt;", str_replace("<", "&lt;", $shortMsg->item(0)->nodeValue));
    //if there is a long message (ie ErrorLevel=1), display it
    if (count($longMsg) > 0)
        echo '<BR>', str_replace(">", "&gt;", str_replace("<", "&lt;", $longMsg->item(0)->nodeValue));
}
else { //no errors
    //get the node containing the time and display its contents
    $eBayTime = $responseDoc->getElementsByTagName('Timestamp');
    echo '<P><B>The Official eBay Time is ', $eBayTime->item(0)->nodeValue, ' GMT</B>';
}
?>

    </BODY>
</HTML>

This is the output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
    <HEAD>
        <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <TITLE>GeteBayOfficialTime</TITLE>
    </HEAD>
    <BODY>

* About to connect() to api.ebay.com port 443 (#0)
*   Trying 66.135.211.100... * Timeout
*   Trying 66.135.211.140... * Timeout
*   Trying 66.211.179.150... * Timeout
*   Trying 66.211.179.180... * Timeout
*   Trying 66.135.211.101... * Timeout
*   Trying 66.211.179.148... * Timeout
* connect() timed out!
* Closing connection #0
<P>Error sending request

Facts:

  1. I've tested this script in 2 completely different servers.
  2. I've made this same call on eBay Online Testing Tool, and it's working fine with my eBay dev keys.
  3. Nothing has changed with the code since the problem started to happen.

Anyone who has had similar issues? Any ideas?

Thanks!

4

2 回答 2

2

Just to update: The problem was between my hosting service (1&1) and eBay. They (1&1) told me eBay every now and again blocks some IP addresses to connect to they API. So it took me a couple of days to be able to connect to the eBay API, but there was nothing wrong with the application. So, sometimes it's worth checking with your server hosting provider as they could be being blocked my eBay.

于 2014-02-12T20:51:58.667 回答
0

从昨天开始,我确实看到了同样的问题——但只在两台服务器上(一台在美国,另一台在澳大利亚),而在其他服务器上没有。

要在这些服务器上复制它,它甚至不必是一个有效的请求 - 只需发送一个 HTTP GET 请求,https://api.ebay.com/wsapi?callname=GeteBayOfficialTime&siteid=0该请求应该返回一个 XML 格式的错误,失败并出现与connect() timed out!您得到的相同错误。

当您在不同的服务器或本地机器上运行相同的代码时会发生什么?难道是 eBay 开始阻止来自某些 IP 的请求?

免责声明:我知道这不是一个答案,但我还不能添加评论,它可能有助于找到一个模式......

于 2013-08-16T16:48:47.197 回答