Skip to content

Instantly share code, notes, and snippets.

@3zzy
Created February 12, 2015 13:31
Show Gist options
  • Save 3zzy/70007216ffe92cbc7992 to your computer and use it in GitHub Desktop.
Save 3zzy/70007216ffe92cbc7992 to your computer and use it in GitHub Desktop.
eBay Get Categories
<?php
header('Content-type: text/plain');
$endpoint = "https://api.ebay.com/ws/api.dll";
$api_dev_name = "secret";
$api_app_name = "secret";
$api_cert_name = "secret";
$auth_token = "token";
$headers = array(
'Content-Type:text/xml',
'X-EBAY-API-COMPATIBILITY-LEVEL: 819',
'X-EBAY-API-DEV-NAME: '.$api_dev_name,
'X-EBAY-API-APP-NAME: '.$api_app_name,
'X-EBAY-API-CERT-NAME :'.$api_cert_name,
'X-EBAY-API-CALL-NAME: GetCategories',
'X-EBAY-API-RESPONSE-ENCODING : JSON',
'X-EBAY-API-REQUEST-ENCODING : JSON',
'X-EBAY-API-SITEID: 15' # eBay.co.uk
);
$body = <<<BODY
<?xml version="1.0" encoding="utf-8"?>
<GetCategoriesRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>$auth_token</eBayAuthToken>
</RequesterCredentials>
<ViewAllNodes>True</ViewAllNodes>
<DetailLevel>ReturnAll</DetailLevel>
</GetCategoriesRequest>
BODY;
$body = utf8_encode($body);
$curl = curl_init();
curl_setopt_array($curl,
array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $endpoint,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $headers
)
);
$response = curl_exec($curl);
curl_close($curl);
if(!$response){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
else
{
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
//print_r($array);
}
function recursive_array_search($needle, $haystack, $currentKey = '') {
foreach($haystack as $key=>$value) {
if (is_array($value)) {
$nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']');
if ($nextKey) {
return $nextKey;
}
}
else if($value==$needle) {
return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey . '["' .$key . '"]';
}
}
return false;
}
function unvar_dump($str) {
if (strpos($str, "\n") === false) {
//Add new lines:
$regex = array(
'#(\\[.*?\\]=>)#',
'#(string\\(|int\\(|float\\(|array\\(|NULL|object\\(|})#',
);
$str = preg_replace($regex, "\n\\1", $str);
$str = trim($str);
}
$regex = array(
'#^\\040*NULL\\040*$#m',
'#^\\s*array\\((.*?)\\)\\s*{\\s*$#m',
'#^\\s*string\\((.*?)\\)\\s*(.*?)$#m',
'#^\\s*int\\((.*?)\\)\\s*$#m',
'#^\\s*bool\\(true\\)\\s*$#m',
'#^\\s*bool\\(false\\)\\s*$#m',
'#^\\s*float\\((.*?)\\)\\s*$#m',
'#^\\s*\[(\\d+)\\]\\s*=>\\s*$#m',
'#\\s*?\\r?\\n\\s*#m',
);
$replace = array(
'N',
'a:\\1:{',
's:\\1:\\2',
'i:\\1',
'b:1',
'b:0',
'd:\\1',
'i:\\1',
';'
);
$serialized = preg_replace($regex, $replace, $str);
$func = create_function(
'$match',
'return "s:".strlen($match[1]).":\\"".$match[1]."\\"";'
);
$serialized = preg_replace_callback(
'#\\s*\\["(.*?)"\\]\\s*=>#',
$func,
$serialized
);
$func = create_function(
'$match',
'return "O:".strlen($match[1]).":\\"".$match[1]."\\":".$match[2].":{";'
);
$serialized = preg_replace_callback(
'#object\\((.*?)\\).*?\\((\\d+)\\)\\s*{\\s*;#',
$func,
$serialized
);
$serialized = preg_replace(
array('#};#', '#{;#'),
array('}', '{'),
$serialized
);
return unserialize($serialized);
}
$categories = array();
$i = 0;
foreach ($array['CategoryArray']['Category'] as $category) {
//echo print_r($category,1);
echo 'category parent ID: '.$category['CategoryParentID']."\n";
$array_search = recursive_array_search($category['CategoryParentID'], $categories);
echo 'Array search: '.$array_search."\n";
$index = isset($array_search) ? $array_search : false;
echo 'index: '.print_r($index)."\n";
if ($index) {
//echo "\n\n=================".
$category['CategoryID'].
' - '.
$category['CategoryName'].
' (PARENT: '. $category['CategoryParentID'].
') =================';
$value = eval('$categories'.$index);
$value[$category['CategoryID']] = $category['CategoryName'];
}
else {
//echo "\n\n=================".$category['CategoryID'].' - '.$category['CategoryName'].'=================';
$categories[$category['CategoryParentID']][$category['CategoryID']] = $category['CategoryName'];
}
}
// echo print_r($categories, 1);
foreach ($categories as $category) {
$i = 1;
foreach ($category as $value) {
if ($i == 1) {
//echo $i.'<br />';
echo $value.'<br />';
}
else {
//echo $i.'<br />';
echo "&nbsp;&nbsp;&nbsp;&nbsp;".$value.'<br />';
}
$i++;
}
echo '<br /><br />';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment