您好我是一名初学程序员,希望将我的XML输出转换为JSON格式,并最终对html进行Ajax调用。 XML输出由Example.php生成,引用了amazon_api_class.php和shown here。我在网站上找到的答案似乎适用于输入和输出xml文件,而在我的场景中我只有输出xml文件。
Here is my XML output(输出太长,不适合这个身体)
这是我的php文件:
Example.php
<?php
require_once('amazon_api_class.php');
$obj = new AmazonProductAPI();
// original
try
{
$result = $obj->getItemByKeyword("X-Men Origins",
AmazonProductAPI::DVD
);
}
catch(Exception $e)
{
echo $e->getMessage();
}
//print_r returns $result in XML format
print_r($result);
//$json = json_encode($result);
//$array = json_decode($json, true);
//echo $array['Items']['Item']['ASIN'];
echo "Sales Rank : {$result->Items->Item->SalesRank}<br>";
echo "ASIN : {$result->Items->Item->ASIN}<br>";
echo "<br><img src=\"" . $result->Items->Item->MediumImage->URL . "\" /><br>";
?>
amazon_api_class.php (引用Example.php)
<?php
require_once 'aws_signed_request.php';
class AmazonProductAPI
{
/**
* Your Amazon Access Key Id
* @access private
* @var string
*/
private $public_key = "AKIAJETPMOLIUXXXXXXX";
/**
* Your Amazon Secret Access Key
* @access private
* @var string
*/
private $private_key = "eHpNEaUwsf+HXXXXXXXXXXQGg7Ic2w+K5Gb6rYa";
/**
* Your Amazon Associate Tag
* Now required, effective from 25th Oct. 2011
* @access private
* @var string
*/
private $associate_tag = "mpXXXX-20";
/**
* Constants for product types
* @access public
* @var string
*/
/*
Only three categories are listed here.
More categories can be found here:
http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/APPNDX_SearchIndexValues.html
*/
const MUSIC = "Music";
const DVD = "DVD";
const GAMES = "VideoGames";
/**
* Check if the xml received from Amazon is valid
*
* @param mixed $response xml response to check
* @return bool false if the xml is invalid
* @return mixed the xml response if it is valid
* @return exception if we could not connect to Amazon
*/
private function verifyXmlResponse($response)
{
if ($response === False)
{
throw new Exception("Could not connect to Amazon");
}
else
{
if (isset($response->Items->Item->ItemAttributes->Title))
{
return ($response);
}
else
{
throw new Exception("Invalid xml response.");
}
}
}
/**
* Query Amazon with the issued parameters
*
* @param array $parameters parameters to query around
* @return simpleXmlObject xml query response
*/
private function queryAmazon($parameters)
{
return aws_signed_request("com", $parameters, $this->public_key, $this->private_key, $this->associate_tag);
}
/**
* Return details of products searched by various types
*
* @param string $search search term
* @param string $category search category
* @param string $searchType type of search
* @return mixed simpleXML object
*/
public function searchProducts($search, $category, $searchType = "UPC")
{
$allowedTypes = array("UPC", "TITLE", "ARTIST", "KEYWORD");
$allowedCategories = array("Music", "DVD", "VideoGames");
switch($searchType)
{
case "UPC" : $parameters = array("Operation" => "ItemLookup",
"ItemId" => $search,
"SearchIndex" => $category,
"IdType" => "UPC",
"ResponseGroup" => "Medium");
break;
case "TITLE" : $parameters = array("Operation" => "ItemSearch",
"Title" => $search,
"SearchIndex" => $category,
"ResponseGroup" => "Medium");
break;
}
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by UPC
*
* @param int $upc_code UPC code of the product to search
* @param string $product_type type of the product
* @return mixed simpleXML object
*/
public function getItemByUpc($upc_code, $product_type)
{
$parameters = array("Operation" => "ItemLookup",
"ItemId" => $upc_code,
"SearchIndex" => $product_type,
"IdType" => "UPC",
"ResponseGroup" => "Medium");
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by ASIN
*
* @param int $asin_code ASIN code of the product to search
* @return mixed simpleXML object
*/
public function getItemByAsin($asin_code)
{
$parameters = array("Operation" => "ItemLookup",
"ItemId" => $asin_code,
"ResponseGroup" => "Medium");
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by keyword
*
* @param string $keyword keyword to search
* @param string $product_type type of the product
* @return mixed simpleXML object
*/
public function getItemByKeyword($keyword, $product_type)
{
$parameters = array("Operation" => "ItemSearch",
"Keywords" => $keyword,
"SearchIndex" => $product_type);
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
}
?>