• 如何获取前端提交来得json格式数据


    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']);

  • 相关阅读:
    Python基础-序列化模块
    dubbox
    小型供销系统
    MyBatis与SpringBoot整合案例(一)
    SpringBoot第二节
    SpringBoot第一节
    Dubbo案例SSM整合
    Dubbo生产者和消费者
    Zookeeper实战分布式锁
    Zookeeper Watcher和选举机制
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13531904.html
Copyright © 2020-2023  润新知