【问题标题】:how to send xml file to amazon mws using php如何使用 php 将 xml 文件发送到亚马逊 mws
【发布时间】:2014-05-24 22:09:53
【问题描述】:

我正在尝试将 xml 文件发送到亚马逊 mws 市场,同时我正在发送查询参数,例如市场 ID、访问密钥等。 下面是代码sn-p,我在本地测试这个sn-p我得到以下错误,

错误:无法回退必要的数据 错误代码:401

    $file   =   "C:\\ListOrdersResponse.xml";
    $fopen  =   fopen($file,"r");

    $urluse =   "https://mws.amazonservices.in";


    $postArray  =   array();
    $postArray['AWSAccessKeyId']    =   'MYKEY';
    $postArray['Action']            =   'SubmitFeed';
    $postArray['FeedType']          =   '_POST_PRODUCT_PRICING_DATA_';
    $postArray['SellerId']          =   'MYSELLERID';
    $postArray['SignatureMethod']   =   'HmacSHA256';
    $postArray['SignatureVersion']  =   2;
    $postArray['Timestamp']         =   gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
    $postArray['Version']           =   '2011-10-01';

    $url_string     =   http_build_query($postArray);
    //signature
    $signature      =   $postArray['Action'] . $postArray['Timestamp'];
    $actualSig      =   base64_encode(hash_hmac("sha256", $signature, $postArray['AWSAccessKeyId'], true));

    $postArray['Signature']         =   $actualSig;

    $httpHeader     =   array();
    $httpHeader[]   =   'Transfer-Encoding: chunked';
    $httpHeader[]   =   'Content-Type: text/xml';
    $httpHeader[]   =   'Content-MD5: ' . base64_encode(md5_file($file, true));
    $httpHeader[]   =   'Expect:';
    $httpHeader[]   =   'Accept:';

    $curl_options   =   array(
        CURLOPT_UPLOAD  =>  true,
        CURLOPT_INFILE  =>  $fopen,
        CURLOPT_RETURNTRANSFER  =>  true,
        //CURLOPT_PUT       =>  TRUE,
        //CURLOPT_POST  =>  true,
        //CURLOPT_PORT  =>  443,
        //CURLOPT_SSLVERSION    =>  3,
        CURLOPT_SSL_VERIFYHOST  =>  false,
        CURLOPT_SSL_VERIFYPEER  =>  false,
        //CURLOPT_FOLLOWLOCATION    =>  1,
        //CURLOPT_PROTOCOLS =>  CURLPROTO_HTTPS,
        CURLINFO_HEADER_OUT =>  TRUE,
        CURLOPT_HTTPHEADER  =>  $httpHeader,
        //CURLOPT_CUSTOMREQUEST =>  'POST',
        //CURLOPT_POSTFIELDS    =>  $url_string,
        CURLOPT_HTTPAUTH    =>  CURLAUTH_ANY,
        CURLOPT_USERPWD => "snapdeal:snapdeal",
        CURLOPT_VERBOSE =>  true,
        CURLOPT_CUSTOMREQUEST   =>  'PUT',
        CURLOPT_HEADER  =>  true
    );

    $ch =   curl_init($urluse.'/?'.$url_string);
    curl_setopt_array($ch,$curl_options);
    $response   =   curl_exec($ch);


    /*$fp   =   fopen("C:/result.xml","wb");
    fwrite($fp,$response);
    fclose($fp);*/
    if (curl_errno($ch)) { 
        print "Error: " . curl_error($ch);
        print_r(curl_getinfo($ch, CURLINFO_HTTP_CODE)); 
    } else { 
        // Show me the result 
curl_close($ch);
    } 

谁能帮我解决这个问题。

【问题讨论】:

  • 你找到答案了吗?

标签: xml curl amazon-mws


【解决方案1】:

“必要的数据倒带是不可能的”错误消息似乎与将 CURLOPT_INFILE 与 http 重定向结合使用有关(http 代码 301 或 302,请参阅 here)

出路要么不使用流(例如,使用 POSTFIELDS 而不是 INFILE),要么提供一个接口,以便 curl 在遇到重定向时可以通过SEEKFUNCTION 倒回流。

以下 sn-p 代码来自积极用于通过 MWS 发送 XML 数据的代码(诚然,不是发往亚马逊印度,如果这有什么不同的话)。它不使用流,因此具有更大的内存开销:

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://'.$AmazonMWShost.'/'.$apiurl.'?'.FieldsToURL($fields));
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: text/xml","Content-MD5: ".$contentmd5,"x-amazon-user-agent: MyScriptName/1.0"));
$result = curl_exec($ch);
curl_close($ch);

【讨论】:

  • 谢谢回复,我需要通过字段发送什么。我正在通过字段发送操作、卖家 ID 等。是否正确。
  • @NagarajuVadlamudi 以上 sn-p 中的$data 是一个包含要发送的 XML 的字符串,$fields 是一个类似于您的 $postArray 的数组。
猜你喜欢
  • 2015-03-21
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
相关资源
最近更新 更多