如何使用Guzzle以JSON格式发送POST请求?

 两斤 发布于 2023-01-19 14:39

有没有人知道post使用JSON 的正确方法Guzzle

$request = $this->client->post(self::URL_REGISTER,array(
                'content-type' => 'application/json'
        ),array(json_encode($_POST)));

我收到internal server error服务器的回复.它适用于Chrome Postman.

7 个回答
  • 简单而基本的方式(guzzle6):

    $client = new Client([
        'headers' => [ 'Content-Type' => 'application/json' ]
    ]);
    
    $response = $client->post('http://api.com/CheckItOutNow',
        ['body' => json_encode(
            [
                'hello' => 'World'
            ]
        )]
    );
    

    要获取响应状态代码和正文的内容,我执行了以下操作:

    echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
    echo '<pre>' . var_export($response->getBody()->getContents(), true) . '</pre>';
    

    2023-01-19 14:41 回答
  • 对于Guzzle <= 4:

    这是一个原始的帖子请求,因此将JSON放入正文解决了问题

    $request = $this->client->post($url,array(
                    'content-type' => 'application/json'
            ),array());
    $request->setBody($data); #set body!
    $response = $request->send();
    
    return $response;
    

    2023-01-19 14:41 回答
  • 这对我有用(使用Guzzle 6)

    $client = new Client(); 
    $result = $client->post('http://api.example.com', [
                'json' => [
                    'value_1' => 'number1',
                    'Value_group' =>  
                                 array("value_2" => "number2",
                                        "value_3" => "number3")
                        ]
                    ]);
    
    echo($result->getBody()->getContents());
    

    2023-01-19 14:41 回答
  • 对于Guzzle 5和6,您可以这样做:

    use GuzzleHttp\Client;
    
    $client = new Client();
    
    $response = $client->post('url', [
        GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar']
    ]);
    

    文件

    2023-01-19 14:41 回答
  • Guzzle 6.2对我有用:

    $gClient =  new \GuzzleHttp\Client(['base_uri' => 'www.foo.bar']);
    $res = $gClient->post('ws/endpoint',
                                array(
                                    'headers'=>array('Content-Type'=>'application/json'),
                                    'json'=>array('someData'=>'xxxxx','moreData'=>'zzzzzzz')
                                    )
                        );
    

    根据文档guzzle做json_encode

    2023-01-19 14:41 回答
  • $client = new \GuzzleHttp\Client();
    
    $body['grant_type'] = "client_credentials";
    $body['client_id'] = $this->client_id;
    $body['client_secret'] = $this->client_secret;
    
    $res = $client->post($url, [ 'body' => json_encode($body) ]);
    
    $code = $res->getStatusCode();
    $result = $res->json();
    

    2023-01-19 14:41 回答
  • $client = new \GuzzleHttp\Client(['base_uri' => 'http://example.com/api']);
    
    $response = $client->post('/save', [
        'json' => [
            'name' => 'John Doe'
        ]
    ]);
    
    return $response->getBody();
    

    2023-01-19 14:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有