2013-11-27 30 views
-2

編輯:前放任何DOWNVOTE,你可以通過我的評論下方。PHP,正從返回的XML元素從亞馬遜產品廣告API

一般如果您願意,您不需要閱讀下面的代碼。我可以從亞馬遜產品廣告API獲取XML響應,並在php中傳遞一個ASIN。所以,如果你願意,你可以跳過代碼部分並閱讀。

function aws_signed_request($region,$params,$public_key,$private_key,$associate_tag) 
    { 

     $method = "GET"; 
     $host = "ecs.amazonaws.".$region; // must be in small case 
     $uri = "/onca/xml"; 


     $params["Service"]   = "AWSECommerceService"; 
     $params["AWSAccessKeyId"] = $public_key; 
     $params["AssociateTag"]  = $associate_tag; 
     $params["Timestamp"]  = gmdate("Y-m-d\TH:i:s\Z"); 
     $params["Timestamp"]  = "2013-11-27T20:52:19Z"; 




     $params["Version"]   = "2009-03-31"; 

     /* The params need to be sorted by the key, as Amazon does this at 
      their end and then generates the hash of the same. If the params 
      are not in order then the generated hash will be different thus 
      failing the authetication process. 
     */ 
     ksort($params); 

     $canonicalized_query = array(); 

     foreach ($params as $param=>$value) 
     { 
      $param = str_replace("%7E", "~", rawurlencode($param)); 
      $value = str_replace("%7E", "~", rawurlencode($value)); 
      $canonicalized_query[] = $param."=".$value; 
     } 

     $canonicalized_query = implode("&", $canonicalized_query); 

     $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query; 

     /* calculate the signature using HMAC with SHA256 and base64-encoding. 
      The 'hash_hmac' function is only available from PHP 5 >= 5.1.2. 
     */ 
     $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True)); 

     /* encode the signature for the request */ 
     $signature = str_replace("%7E", "~", rawurlencode($signature)); 

     /* create request */ 
     $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature; 

     /* I prefer using CURL */ 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$request); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

     $xml_response = curl_exec($ch); 

     /* If cURL doesn't work for you, then use the 'file_get_contents' 
      function as given below. 
     */ 

     if ($xml_response === False) 
     { 
      return False; 
     } 
     else 
     { 
      /* parse XML */ 
      $parsed_xml = @simplexml_load_string($xml_response); 
      return ($parsed_xml === False) ? False : $parsed_xml; 
     } 
    } 


class AmazonProductAPI{ 

     private function queryAmazon($parameters) 
         { 
          return aws_signed_request("com", $parameters, $this->public_key, $this->private_key, $this->associate_tag); 
         } 



     public function getItemByAsin($asin_code) 
        { 
         $parameters = array("Operation"  => "ItemLookup", 
              "ItemId"  => $asin_code, 
              //"ResponseGroup" => "Medium" 
              "ResponseGroup" => "ItemAttributes" 
              //"ResponseGroup" => "ItemDimensions" 

              ); 

         $xml_response = $this->queryAmazon($parameters); 

         //var_dump($xml_response); 

         //$result= $this->verifyXmlResponse($xml_response); 

         //var_dump($result); 

         return $xml_response; 

        } 


      } 

調用方法信息WRT的ASIN:

$obj = new AmazonProductAPI(); 
$result= $obj->getItemByAsin("1594486344"); 

是一切正常。可以使用print_r($result)打印XML響應。但問題是,如何從PHP變量中的返回的XML中獲取重量,寬度,高度和長度?更具體地說,我如何解析XML在PHP中的反響?

+0

已經3個downvotes已經鑄造。我知道網上有腳本,但它們不起作用。這是一個這樣的腳本:http://www.bloggingline.com/tutorials/parsing-amazon-xml-api-using-php.html –

回答

0

永遠不要忘記你最好的朋友,谷歌和手冊。

其中一個選項是XML parser,第二個是SimpleXML。兩個 的例子是在手動可用,但這裏是兩行用SimpleXML:

$items = new SimpleXMLElement($xmlstr); 
echo $items->item[0]->someValue; 
+0

XML表示法是通過'simplexml_load_string()'獲得的;那麼爲什麼要使用'新的SimpleXMLElement($ xmlstr);'? –

+0

@IstiaqueAhmed因爲這是可能的方法之一,我喜歡的對象不僅僅是函數;) –

+0

'SimpleXMLElement'作爲它的參數是一個XML文件。我怎樣才能得到這個Xml已經被'simplexml_load_string()'轉換成了一個對象?從http://www.w3schools.com/php/func_simplexml_load_string.asp:「轉換一個良好的XML字符串轉換成SimpleXMLElement對象,那麼輸出鍵和對象的元素:」 –

相關問題