download1323 2013-11-27 11:27
浏览 55

php,从AMAZON产品广告API中获取返回XML中的元素

EDIT: BEFORE PUTTING ANY DOWNVOTE, YOU CAN GO THROUGH MY COMMENT BELOW.

Acually YOU DO NOT NEED TO READ the code below if you wish to do so. I can get the XML response from the Amazon Product Advertising API passing an ASIN in php. So if you wish , you can skip the code part and read on.

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."
".$host."
".$uri."
".$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;

                    }


            }

Calling the method to info wrt an ASIN :

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

Yes everything is ok. XML reponse can be printed with print_r($result). But the thing is that how can I get the weight, width, height , length from the returned XML in php variables? More specifically , how can I parse the XML resonse in php ?

  • 写回答

1条回答 默认 最新

  • dsuxcxqep31023992 2013-11-27 11:45
    关注

    Never forget about your best friends, google and manual.

    One of the options is XML parser and second is SimpleXML. Examples for both are available in manual, but here is two lines with SimpleXML:

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

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据