私が行ったいくつかの自動化されたテストでは、Chrome からのリクエストを記録し、それらを curl コマンドで繰り返す必要がありました。私はそれを行う方法をチェックし始めます...
4069 次
3 に答える
9
私がやった方法は次のとおりです。
- 開発者ツールを開いたときに Web サイトにアクセスします。
- リクエストを発行し、それらがコンソールに記録されていることを確認します。
- リクエストを右クリックし、[コンテンツを含む HAR として保存] を選択して、ファイルに保存します。
- 次に、次の php スクリプトを実行して HAR ファイルを解析し、正しいカールを出力します。
脚本:
<?php
$contents=file_get_contents('/home/elyashivl/har.har');
$json = json_decode($contents);
$entries = $json->log->entries;
foreach ($entries as $entry) {
$req = $entry->request;
$curl = 'curl -X '.$req->method;
foreach($req->headers as $header) {
$curl .= " -H '$header->name: $header->value'";
}
if (property_exists($req, 'postData')) {
# Json encode to convert newline to literal '\n'
$data = json_encode((string)$req->postData->text);
$curl .= " -d '$data'";
}
$curl .= " '$req->url'";
echo $curl."\n";
}
于 2015-09-17T07:40:02.700 に答える
1
ElyashivLavi によるコードに基づいて、ファイル名の引数、ファイルからの読み取り時のエラー チェック、curl を詳細モードに設定、Accept-encoding リクエスト ヘッダーの無効化を追加しました。デバッグ、およびcurlコマンドの自動実行:
<?php
function bail($msg)
{
fprintf(STDERR, "Fatal error: $msg\n");
exit(1);
}
global $argv;
if (count($argv) < 2)
bail("Missing HAR file name");
$fname = $argv[1];
$contents=file_get_contents($fname);
if ($contents === false)
bail("Could not read file $fname");
$json = json_decode($contents);
$entries = $json->log->entries;
foreach ($entries as $entry)
{
$req = $entry->request;
$curl = 'curl --verbose -X '.$req->method;
foreach($req->headers as $header)
{
if (strtolower($header->name) === "accept-encoding")
continue; // avoid gzip response
$curl .= " -H '$header->name: $header->value'";
}
if (property_exists($req, 'postData'))
{
# Json encode to convert newline to literal '\n'
$data = json_encode((string)$req->postData->text);
$curl .= " -d '$data'";
}
$curl .= " '$req->url'";
echo $curl."\n";
system($curl);
}
于 2020-04-09T19:39:37.950 に答える