如何使用PHP将xml文件发送到amazon mws

时间:2014-05-22 09:40:59

标签: xml curl amazon-mws

我正在尝试将xml文件发送到amazon mws marketplace以及我发送查询参数,例如marketplace id,accesskey等。 下面是代码片段,我在本地测试这个片段我收到以下错误,

错误:无法进行必要的数据重绕 错误代码: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);
    } 

任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

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

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

以下代码段来自正在积极用于通过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);