jeudi 14 juin 2018

why an http post request isn't working in guzzle but works fine in curl

i am working on sending a request to github o auth to fetch the access token of a user and tried working with guzzle but the response didn't have any value i don't know why , in the other hand when i used curl it worked fine and returned the access token successfully , i am using form_parms from guzzle to set the fields

here the guzzle example

public function fetchGithubToken(\Illuminate\Http\Request $request, $code)
    {


        $client = new \GuzzleHttp\Client();

        $response = $client->request('POST', 'https://github.com/login/oauth/access_token', [

            'form_params' => ['client_id' => env('GITHUB_CLIENTID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'code' => $code],
        ]);

        Log::error(print_r($response, true));
        return $response;


    }

here the is the curl example

    public function post_to_url($url, $data)
{
    $fields = '';
    foreach ($data as $key => $value) {
        $fields .= $key . '=' . $value . '&';
    }
    rtrim($fields, '&');

    $post = curl_init();

    curl_setopt($post, CURLOPT_URL, $url);
    curl_setopt($post, CURLOPT_POST, count($data));
    curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($post);

    curl_close($post);

    return $result;

}

public function fetchGithubToken(\Illuminate\Http\Request $request, $code)
{

    $data = ['client_id' => env('GITHUB_CLIENTID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'code' => $code];

    $response = $this->post_to_url("https://github.com/login/oauth/access_token", $data);

    Log::info(print_r($response, true));

    return $response;
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2JD8blF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire