Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 799
Yusuf Ibrahim Avatar asked Dec 22 '14 04:12

Yusuf Ibrahim


2 Answers

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();
like image 107
Yusuf Ibrahim Avatar answered Sep 22 '22 02:09

Yusuf Ibrahim


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();
like image 25
Sébastien Gicquel Avatar answered Sep 21 '22 02:09

Sébastien Gicquel