【问题标题】:Guzzle POST request php vs AjaxGuzzle POST 请求 php vs Ajax
【发布时间】:2017-01-17 03:16:07
【问题描述】:

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

阿贾克斯

            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();

【问题讨论】:

  • 什么版本的 Guzzle?
  • 版本 6.2.2 .

标签: jquery ajax laravel guzzle


【解决方案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'
                                )
                            ));

【讨论】:

    猜你喜欢
    • 2018-02-23
    • 1970-01-01
    • 2018-06-16
    • 2017-01-05
    • 1970-01-01
    • 2020-07-14
    • 2012-09-04
    • 2016-05-20
    • 1970-01-01
    相关资源
    最近更新 更多