常用的职位表数据

问题描述 投票:6回答:2

我使用的是Httpful PHP库 http:/phphttpclient.com 这里是我的示例代码。

$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与表单数据内的请求。

谁能帮帮我...

php http post request httpful
2个回答
8
投票

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

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

0
投票

以下是我如何发布数据。

$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();
© www.soinside.com 2019 - 2024. All rights reserved.