1

我正在使用 browserstack screenshots API - https://www.browserstack.com/screenshots/api以下 curl 正在工作:

curl -u "username:password" -H "Content-Type: application/json" -H "Accept: application/json"  -d '{"browsers": [{"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"}], "url": "http://google.com"}' http://www.browserstack.com/screenshots

但是,当我尝试使用 guzzle 进行相同的调用时,我得到 422 Unprocessable Entity 错误。

$client = new GuzzleHttp\Client();
$request = $client->post('http://www.browserstack.com/screenshots', [
        'headers' => ['Content-type' => 'application/json'],
        'auth' =>  ['username', 'password']
]
);

$data = ['browsers' => ['os' => 'Windows', 'os_version' => '7', 'browser_version' => '8.0', 'browser' => 'ie'], 'url' => 'http://google.com'];
$request->setBody($data);
$response = $request->send();
dd($response);

你能建议如何调试这个问题吗?

4

3 回答 3

3

我的方法有误。它应该是$client->createRequest而不是$client->post 另外,我在传递数据时出错了。浏览器应该是数组数组

还有另一个库可以与 browserstack 和 guzzle 一起使用:https ://github.com/ksenzee/browserstack-screenshots-php

$client = new GuzzleHttp\Client();

$request = $client->createRequest('POST', 'http://www.browserstack.com/screenshots', [
        'headers' => ['Content-type' => 'application/json'],
        'auth' =>  ['user', 'pwd'],
        'body' => '{"browsers": [
                       {"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
                       {"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
                ],
                "url": "http://www.lipsum.com"}'
]
);
$response = $client->send($request);
dd($response->json());
于 2014-10-28T20:28:40.550 回答
2

要将 JSON 发送到 Screenshots API,您需要将其格式化为 JSON 字符串。

$client = new GuzzleHttp\Client();
$request = $client->post('http://www.browserstack.com/screenshots', [
        'headers' => ['Content-type' => 'application/json'],
        'auth' =>  ['username', 'access_key'],
        'body' => '{"browsers": [
                           {"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
                           {"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
                    ],
                    "url": "http://www.lipsum.com"}'
            ]
        );

然后,您将能够在BrowserStack Screenshots页面上查看进度。

有关如何使用 Guzzle 发送 POST 请求的指南,您可以参考此文档 — https://media.readthedocs.org/pdf/guzzle/latest/guzzle.pdf

于 2014-10-28T17:37:12.227 回答
0

在我的情况下,在选项的参数中使用“json”(https://es.stackoverflow.com/questions/185183/porqu%C3%A9-guzzle-5-0-lanza-el-error-422-si-estoy-armando -bien-la-consulta#185192),例如:

$client = new GuzzleHttp\Client(['base_url' => 'http://www.browserstack.com/']);
$request = $client->post('screenshots', [
    'headers' => ['Content-type' => 'application/json'],
    'auth' =>  ['username', 'access_key'],
    'json' => '{"browsers": [
                       {"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
                       {"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
                ],
                "url": "http://www.lipsum.com"}'
        ]
    );
于 2018-07-31T14:16:37.773 回答