Httpful post form data

11,157

Solution 1

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

Solution 2

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();
Share:
11,157
Yusuf Ibrahim
Author by

Yusuf Ibrahim

Java, PHP, and Javascript (Symfony, Laravel, VueJS, and AngularJS ) fullstack Web developer from Bandung Indonesia

Updated on June 12, 2022

Comments

  • Yusuf Ibrahim
    Yusuf Ibrahim almost 2 years

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

  • Pascal_dher
    Pascal_dher over 8 years
    Don't redeclare the data structure in the body, let php do it: ->body(http_build_query($data))