Search code examples
phphttppostrequesthttpful

Httpful post form data


I am using Httpful PHP library from http://phphttpclient.com/ , here is my sample code :

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

my question is how to add form data . I tried to read the documentation but I cannot found any explanation, there are only send xml and Json example, but I cannot get normal POST with form data inside a request.

somebody please help me..


Solution

  • finally I found the answer, thanks to @Xiquid that guide me to find the answer, here is my working answer for sending post data using php httpful rest client :

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