Guzzle POST请求php vs Ajax

时间:2017-01-16 19:01:56

标签: jquery ajax laravel guzzle

我的请求适用于ajax,但是当我尝试用guzzle重新创建场景时失败了。我收到400回复的错误。我有一种感觉,我可能会在guzzle参数中设置错误?这是我第一次使用它。

的Ajax

            var usernameV5 = '108357166';
            var passwordV5 = '1234';

            var data = {
                'type':'base64', 
                'value':btoa(usernameV5 + ":" + passwordV5), 
                'namespace':'https://somelink/customers'
            };
            data = JSON.stringify(data);
            $.ajax({
                'type':'POST',
                'headers':{'Content-Type':'application/json'},
                'url':'cantshow.com',
                'data':data,
                'success':function (result) {

                },
                'error':function () {

                }
            });

狂饮

                            $username = '108357166';
                            $password = '1234';
                            $client = new \GuzzleHttp\Client();
                            $result = $client->post('cantshow.com', [
                                'headers' => [
                                    'Content-Type' => 'application/json'
                                ],
                                'data' => [
                                    'type' => 'base64', 
                                    'value' => base64_encode("'".$username.":".$password."'"), 
                                    'namespace' => 'https://somelink/customers'
                                ]
                            ])->getBody();

1 个答案:

答案 0 :(得分:1)

取决于使用的guzzle版本,对于5和6这应该有效:

$client->post('cantshow.com', array(
                                'headers'=>array('Content-Type'=>'application/json'),
                                'json' => array( 
                                    'type' => 'base64', 
                                    'value' => base64_encode("'".$username.":".$password."'"), 
                                    'namespace' => 'https://somelink/customers'
                                )
                            ));