composer.json
{ "require": { "guzzlehttp/guzzle": "~6.0" } }
composer install
index.php 访问该页面 请求后台接口
<?php require "./vendor/autoload.php"; use GuzzleHttpClient; $baseurl = "http://wang.com/json.php"; $client = new Client(); $response = $client->request('POST', $baseurl, [ 'json' => ['foo' => 'bar'] ]); echo $response->getBody(); echo $response->getHeader('Content-Length');
当我使用 form表单格式请求 或者get query
$client->request('GET', 'http://httpbin.org', [
'query' => ['foo' => 'bar']
]);
$res = $client->request('POST', 'http://localhost:9999/form.php', [
'form_params' => [
'field_name' => 'abc',
'other_field' => '123',
'nested_field' => [
'nested' => 'hello'
]
]
后台都能通过 $_GET $_POST拿到数据
但是 改为json请求就拿不到
结果发现 获取json请求内容要换种
var_dump(file_get_contents('php://input'));
echo json_encode(['code'=>200,'msg'=>'success']);