【问题标题】:Httpful post form dataHttpful post表单数据
【发布时间】:2023-03-09 19:27:02
【问题描述】:

我正在使用来自 http://phphttpclient.com/ 的 Httpful PHP 库,这是我的示例代码:

$data =  array(
            'code'          => $request->query->get('code'),
            'client_id'     => $this->container->getParameter('GOOGLE_CLIENT_ID'),
            'client_secret' => $this->container->getParameter('GOOGLE_CLIENT_SECRET'),
            'redirect_uri'  => $google_redirect,
            'grant_type'    => "authorization_code"
        );

    $response = Request::post($url)->body($data)->sendsType(Mime::FORM)->send();

    var_dump($response);
    die();

我的问题是如何添加表单数据。 我试图阅读文档,但找不到任何解释,只有发送 xml 和 Json 示例,但我无法在请求中获取带有表单数据的正常 POST。

有人请帮帮我..

【问题讨论】:

  • 您是否尝试按照您发布的页面中的示例发布 JSON 字符串?可能类似于Request::post($url)->body('{"name":"josep","age":19}')->sendsJson()->send()
  • 那么用 JSON 字符串尝试页面中的示例。所以->body(json_encode($data))->sendsJson()->send()
  • 我试过 $response = RestRequester::post($url)->body(json_encode($data))->sendsJson()->send();但仍然无法正常工作“缺少必需的参数:grant_type”
  • 您在代码中拼错了grant_type。你有它grand_type
  • 哦,对不起,我更新了我的代码,现在我有了 grant_type ,但对我来说仍然没有运气,它仍然是错误“缺少必需的参数:grant_type”

标签: php http post request httpful


【解决方案1】:

终于我找到了答案,感谢@Xiquid 指导我找到答案,这是我使用 php httpful rest 客户端发送帖子数据的工作答案:

$google_redirect = $request->getSchemeAndHttpHost().$this->generateUrl('myroutename')."?platform=google"; 
        $url =  "https://www.googleapis.com/oauth2/v3/token";

        $data =  array(
            'code'          => $request->query->get('code'),
            'client_id'     => $this->container->getParameter('GOOGLE_CLIENT_ID'),
            'client_secret' => $this->container->getParameter('GOOGLE_CLIENT_SECRET'),
            'redirect_uri'  => $google_redirect,
            'grant_type'    => "authorization_code"
        );


        $response = RestRequester::post($url)
                ->method(Http::POST)        // Alternative to Request::post
                ->withoutStrictSsl()        // Ease up on some of the SSL checks
                ->expectsJson()             // Expect HTML responses
                ->sendsType(Mime::FORM)

                ->body('grant_type=authorization_code&code='.$data['code']."&client_id=".$data['client_id']."&client_secret=".$data['client_secret']."&redirect_uri=".$data['redirect_uri'])
                ->send();

        var_dump($response);
        die();

【讨论】:

  • 不要在body中重新声明数据结构,让php来做:->body(http_build_query($data))
【解决方案2】:

这是我发布数据的方式:

$response = \Httpful\Request::post($uri)
                    ->body([
                        'hello' => 'world',
                        'lorem' => 'ipsum',
                        'Date' => '2020-04-29',
                            ], \Httpful\Mime::FORM)
                    ->send();

如果我需要发布 JSON:

$response = \Httpful\Request::post($uri)
                    ->sendsJson()
                    ->body([
                        'hello' => 'world',
                            'lorem' => 'ipsum',
                            'Date' => '2020-04-29',
                            ], \Httpful\Mime::JSON)
                    ->send();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-08
    • 2011-08-11
    • 2014-12-19
    • 2012-07-26
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多