gpt4 book ai didi

php - 如何使用 Guzzle 6 发送表单字段?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:27 24 4
gpt4 key购买 nike

我正在为在 Symfony4 中创建的 API 开发我的单元测试

阅读 Guzzle 文档我生成了以下代码:

文件 SecurityControllerTest.php

    $client = new Client([
'base_uri' => 'http://localhost/sacrepad/sacrepad-api/public/index.php/',
'timeout' => 2.0,
]);
$data = array();
$data['email'] = 'admin@admin.com';
$data['password'] = '12345678';
$data2 = array();
$data2['json'] = $data;
$formData = json_encode($data);
$response = $client->request('POST', 'login', [
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
'json' => $formData,
]
]);
$body = json_decode($response->getBody(), true);

文件SecurityController.php

/**
* @Route("/login", name="login", methods={"POST"})
*/
public function login(Request $request,Helpers $helpers,ValidatorInterface $validator, JwtAuth $jwtauth) {

$data = array(
'status' => 'error',
'code' => 400,
'msg' => 'data not received'
);

$json = $request->request->get('json');
$params = json_decode($json);
}

当我使用 phpunit 命令运行测试时,出现以下错误:

1) App\Tests\SecurityControllerTest::testAuth GuzzleHttp\Exception\ServerException: Server error: `POST http://localhost/sacrepad/sacrepad-api/public/index.php/login` resulted in a `500 Internal Server Error` response:

如果我更改请求的名称:

$json = $request->request->get('json2');

它有效并返回以下内容:

array(3) {
["status"]=>
string(5) "error"
["code"]=>
int(400)
["msg"]=>
string(18) "data not received"
}

关于如何让它工作和发送参数有什么想法吗?

最佳答案

我构建了一个使用 guzzle 的类

 use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;


class Api
{

protected $client;
protected $url;

public function __construct()
{
$this->client = new Client([
'verify'=>false
]);
$this->url = 'http://localhost/sacrepad/sacrepad-api/public/';

}

public function get($endpoint, $params = [], $headers = [])
{
$response = $this->sendRequest(
'GET',
$this->url . $endpoint,
$params,
$headers
);
return $response;
}

public function post($endpoint, $params = [], $headers = [])
{

$response = $this->sendRequest(
'POST',
$this->url . $endpoint,
$params,
$headers
);

return $response;
}

public function sendRequest($type, $url, $params = [], $headers = [])
{

if ($type == 'GET') {
$data = [
'query' => $params
];
} elseif ($type == 'FILE') {
$type = 'POST';
$data = [
'multipart' => $params // TODO implements later
];
} else {
$data = [
'json' => $params
];
}

if (!empty($headers)) {
$data['headers'] = $headers;
}

$data['headers']['X-REAL-IP'] = $_SERVER['REMOTE_ADDR'];
$data['headers']['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];;
$data['headers']['X-Platform'] = 'web';

try {


$response = $this->client->request(
$type,
$url,
$data
);


if (in_array($response->getStatusCode(), ['200', '403', '404'])) {
return json_decode($response->getBody());
}


return false;
} catch (RequestException $re) {

if (in_array($re->getResponse()->getStatusCode(), ['403', '404', '422'])) {
return json_decode($re->getResponse()->getBody());
}
return json_decode($re->getResponse()->getBody());
} catch (Exception $e) {
return false;
}
}
}

当我想发送一个帖子请求时,它会是这样的

$response = (new Api())->post('index.php/',[
'email'=> 'admin@admin.com',
'password' => '123456'
]);

现在它将发送一个 post 请求到 index.php 并发送电子邮件和密码数据我希望这会有所帮助

关于php - 如何使用 Guzzle 6 发送表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56911084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com