Google OAuth2 curl


I’m working in Google OAuth2 and I am stuck in this part:

Using OAuth 2.0 to Access Google APIs - Authentication and Authorization for Google APIs - Google Code

«You can try out that call using the curl command line application, substituting the code you got from the above example URL—note that you may need to urlencode your redirect and secret, as curl won’t automatically do that for you:

POST /o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded


curl -d

What am I doing wrong?
Not receiving a postive reply from the curl

$url = ''.urlencode('code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&');


function get_web_page( $url )
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;

In this line:

$url = ''.urlencode('code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&');

You are urlencoding the entire query string. You don’t want to do that, you only want to urlencode the values. i.e.

$url = '';
$url .= '?code='.urlencode('4/P7q7W91a-oMsCeLvIaQm6bTrgtp6');
$url .= '&client_id='.urlencode('');
$url .= '&client_secret='.urlencode('XTHhXh1SlUNgvyWGwDk1EjXB');

also, if that is supposed to be submitted via a post request, you need to ONLY use the base url for the curl_init() function, the create the postfields and set that as an option, i.e.:

$url = '';

$postString = 'code='.urlencode('4/P7q7W91a-oMsCeLvIaQm6bTrgtp6');
$postString .= '&client_id='.urlencode('');
$postString .= '&client_secret='.urlencode('XTHhXh1SlUNgvyWGwDk1EjXB');
curl_setopt($request, CURLOPT_POSTFIELDS, $postString);

hope that makes sense