doukenqiong0588 2017-05-07 18:13
浏览 715

使用Guzzle或Curl模拟HTTP请求

I'm trying to replicate the following HTTP request made by Amazon's UI tool to access their Marketplace Web Services (MWS API)

This is the request made when using their UI tool. Note, the HTTP request info was taken from Google Chrome's developer console.

General

Request URL:https://mws.amazonservices.com/Products/2011-10-01

Request Method: POST

Status Code:200 OK

Remote Address:54.239.24.6:443

Referrer Policy:no-referrer-when-downgrade

Request Headers

POST /Products/2011-10-01 HTTP/1.1

Host: mws.amazonservices.com

Connection: keep-alive

Content-Length: 309

Origin: https://mws.amazonservices.com

x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

Accept: text/plain, /

X-Requested-With: XMLHttpRequest

Referer: https://mws.amazonservices.com/scratchpad/index.html

Accept-Encoding: gzip, deflate, br

Accept-Language: en-US,en;q=0.8

Cookie: csm-hit=20.82|1494090798243

Form Data

AWSAccessKeyId: {{API KEY}}

Action:GetMatchingProductForId

SellerId: {{API KEY}}

SignatureVersion:2

Timestamp:2017-05-06T17:43:36Z

Version:2011-10-01

Signature:wVax9gw0DfIfeL7s4Z2IZynwwryi0c6tIkfE7IQY1RI=

SignatureMethod:HmacSHA256

MarketplaceId:ATVPDKIKX0DER

IdType:UPC

IdList.Id.1:034264456730

This is my PHP script using GuzzleHTTP library to try and emulate the request.

 <?php

require './vendor/autoload.php';

use GuzzleHttp\Client;

$body = 'AWSAccessKeyId=AKIAJFTQQL6UN7RO6WBQ&Action=GetMatchingProductForId&SellerId=AWWNSYIT4NM3&SignatureVersion=2&Timestamp=2017-05-06T17%3A43%3A36Z&Version=2011-10-01&Signature=wVax9gw0DfIfeL7s4Z2IZynwwryi0c6tIkfE7IQY1RI%3D&SignatureMethod=HmacSHA256&MarketplaceId=ATVPDKIKX0DER&IdType=UPC&IdList.Id.1=034264456730';

$jar = new \GuzzleHttp\Cookie\CookieJar();

$client = new Client([
    'base_uri'=>'https://mws.amazonservices.com',
    'cookies'=>$jar,
    'headers'=>[
        'User-Agent'=>'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
        'Host'=> 'mws.amazonservices.com',
        'Connection'=>'keep-alive',
        'Content-Length'=>'309',
        'Origin'=>'https://mws.amazonservices.com',
        'x-amazon-user-agent'=>'AmazonJavascriptScratchpad/1.0 (Language=Javascript)',
        'Content-Type'=>'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept'=>'text/plain, */*',
        'X-Requested-With'=>'XMLHttpRequest',
        'Referer'=>'https://mws.amazonservices.com/scratchpad/index.html',
        'Accept-Encoding'=>'gzip, deflate, br',
        'Accept-Language'=>'en-US,en;q=0.8',
        ],
    'body'=>$body,
    ]);

$response = $client->request('POST', 'https://mws.amazonservices.com/Products/2011-10-01');

echo $response->getStatusCode();

?>
  • 写回答

1条回答 默认 最新

  • dongxiequ3724 2017-05-08 13:25
    关注

    Try to use form_params instead of body. And check the response, maybe Amazon says more than just 400 Bad Request (response could contain more details about the error).

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏