6

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..

Yusuf Ibrahim
  • 1,411
  • 2
  • 19
  • 39
  • Did you try posting a JSON string as per the examples from that page you posted? Probably something like `Request::post($url)->body('{"name":"josep","age":19}')->sendsJson()->send()` – Paul Carlton Dec 22 '14 at 05:27
  • 1
    Well try the example in the page with a JSON string. so `->body(json_encode($data))->sendsJson()->send()` – Paul Carlton Dec 22 '14 at 05:31
  • i tried $response = RestRequester::post($url)->body(json_encode($data))->sendsJson()->send(); but still not work "Required parameter is missing: grant_type" – Yusuf Ibrahim Dec 22 '14 at 05:35
  • 1
    You misspelled `grant_type` in your code. You have it as `grand_type` – Paul Carlton Dec 22 '14 at 05:53
  • oh, sorry I updated my code now I have grant_type , but still no luck for me it is still error "Required parameter is missing: grant_type" – Yusuf Ibrahim Dec 22 '14 at 06:03
  • i found the answer ->body('grant_type=authorization_code&code='.$data['code']."&client_id=".$data['client_id']."&client_secret=".$data['client_secret']."&redirect_uri=".$data['redirect_uri']) .. thanks to @Xiquid for guiding me to find this answer – Yusuf Ibrahim Dec 22 '14 at 06:37
  • No problem! Glad I could help! :) – Paul Carlton Dec 22 '14 at 21:21

2 Answers2

9

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();
Yusuf Ibrahim
  • 1,411
  • 2
  • 19
  • 39
0

Here is how I post data :

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

If I need to post JSON :

$response = \Httpful\Request::post($uri)
                    ->sendsJson()
                    ->body([
                        'hello' => 'world',
                            'lorem' => 'ipsum',
                            'Date' => '2020-04-29',
                            ], \Httpful\Mime::JSON)
                    ->send();
Sébastien Gicquel
  • 3,651
  • 6
  • 43
  • 77