Skip to content

Instantly share code, notes, and snippets.

@ali-cedcoss
Last active October 6, 2021 06:39
Show Gist options
  • Save ali-cedcoss/5283542cd125750b496fee56d14c109c to your computer and use it in GitHub Desktop.
Save ali-cedcoss/5283542cd125750b496fee56d14c109c to your computer and use it in GitHub Desktop.
ebay api request using wp_remote_post
<?php
class Cedrequest {
public $devID;
public $appID;
public $certID;
public $serverUrl;
public $compatLevel;
public $siteID;
public $verb;
public $ebayConfigInstance;
public $oauthCodeGrantUrl;
/** __construct
Constructor to make a new instance of eBaySession with the details needed to make a call
Note that authentication credentials (normally token, but could be username and password)
are assumed to come in the request body, and not in the constructor args
Input: $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( $siteToUseID, $callName ) {
$this->loadDepenedency();
$this->devID = $this->ebayConfigInstance->devID;
$this->appID = $this->ebayConfigInstance->appID;
$this->oauthCodeGrantUrl = $this->ebayConfigInstance->oauthCodeGrantUrl;
$this->certID = $this->ebayConfigInstance->certID;
$this->compatLevel = $this->ebayConfigInstance->compatLevel;
$this->siteID = $siteToUseID;
$this->verb = $callName;
$this->serverUrl = $this->ebayConfigInstance->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 ) {
// $headers = $this->buildEbayHeaders();
$response = wp_remote_post( $this->serverUrl, array(
'body' => $requestBody,
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'X-EBAY-API-COMPATIBILITY-LEVEL' => $this->compatLevel,
'X-EBAY-API-DEV-NAME' => $this->devID,
'X-EBAY-API-APP-NAME' => $this->appID,
'X-EBAY-API-CERT-NAME' => $this->certID,
'X-EBAY-API-CALL-NAME' => $this->verb,
'X-EBAY-API-SITEID' => $this->siteID,
],
'httpversion' => '1.0',
'sslverify' => false,
) );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return $error_message;
} else {
return $this->ParseResponse( $response['body'] );
}
}
public function ParseResponse( $responseXml ) {
$sxe = new SimpleXMLElement( $responseXml );
$res = json_decode( json_encode( $sxe ), true );
return $res;
}
/**
* Function loadDepenedency
*
* @name loadDepenedency
*/
public function loadDepenedency() {
if ( is_file( __DIR__ . '/ebayConfig.php' ) ) {
require_once 'ebayConfig.php';
$this->ebayConfigInstance = Ebayconfig::get_instance();
}
if ( is_file( __DIR__ . '/MultiPartMessage.php' ) ) {
require_once 'MultiPartMessage.php';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment