使用Guzzle或Curl模拟HTTP请求

时间:2017-05-07 18:13:26

标签: php curl xmlhttprequest httprequest guzzle

我尝试复制亚马逊UI工具发出的以下HTTP请求,以访问其市场网络服务(MWS API)

这是使用其UI工具时发出的请求。请注意,HTTP请求信息来自Google Chrome的开发者控制台。

常规

请求网址:https://mws.amazonservices.com/Products/2011-10-01

请求方法:POST

状态代码:200 OK

远程地址:54.239.24.6:443

推荐人政策:no-referrer-when-downgrade

请求标题

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

主持人:mws.amazonservices.com

连接:保持活力

内容长度:309

原产地:https://mws.amazonservices.com

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

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

Content-Type:application / x-www-form-urlencoded;字符集= UTF-8

接受:text / plain, /

X-Requested-With:XMLHttpRequest

参考者:https://mws.amazonservices.com/scratchpad/index.html

接受编码:gzip,deflate,br

接受语言:en-US,en; q = 0.8

Cookie:csm-hit = 20.82 | 1494090798243

表单数据

AWSAccessKeyId:{{API KEY}}

动作:GetMatchingProductForId

SellerId:{{API KEY}}

SignatureVersion:2

时间戳:2017-05-06T17:43:36Z

版本:2011-10-01

签名:wVax9gw0DfIfeL7s4Z2IZynwwryi0c6tIkfE7IQY1RI =

是SignatureMethod:HmacSHA256

MarketplaceId:ATVPDKIKX0DER

IdType:UPC

IdList.Id.1:034264456730

这是我的PHP脚本,使用GuzzleHTTP库来尝试模拟请求。

 <?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();

?>

0 个答案:

没有答案