too long

Building a reddit bot.

Disregard the actual API for a second - this cURL handle does not seem to even execute - I can't see any request/response in the browsers's DevTools.

<?php

/** 
* Reddit PHP WikiBot
* 
* @author tomgs
* @link MYSITE.com
*/

/**
* Authentication Using OAUth 2.0 - Step 1
*
* Get Code, State & Refresh Token From Reddit
* @link https://github.com/reddit/reddit/wiki/OAuth2#authorization
*
*/

//Initialize cURL
$ch = curl_init();

/* Set cURL Vars */

//The URL To Get The Code, State & Refresh Token From
$authorizeUrl = 'https://www.reddit.com/api/v1/authorize';
curl_setopt($ch, CURLOPT_URL, $authorizeUrl);

//If The cURL Fails- Die & Throw an Error
//curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);

//Include The Header in The Output
curl_setopt($ch, CURLOPT_HEADER, TRUE);

//Do A POST request Indtead Of The Default GET Request
curl_setopt($ch, CURLOPT_POST, 6);

//Return The Transfer as a String Of The Return Value
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


/**
* Parameters For POST Request - Explained
*
* @param string $client_id You App's Client ID
* @param string $response_type Set To "Code" - For This Step
* @param string $state A Random String, Chosen By Me.
* @param string $duration Set To Permanent, Because We Want A Refresh Token To Be Received From The Server (More About The Refresh Token Later)
* @param string $redirect_uri Where To Go If The Authentication Was Successful
*
*
*
*/

//Some Longer/More Complex Strings - Stored In Local Variables
$client_id = 'CLIENTID';
$redirect_uri = "http://localhost/redditbot2/callback.php";
$random_state = rand();


$params = array(
    "client_id" => $client_id,
    "response_type" => "code",
    "state" => $random_state,
    "redirect_uri" => urlencode($redirect_uri),
    "duration" => "permanent",
    "scope" => "submit"
    );

$params = http_build_query($params);

// Set All The Fields For The Post Request
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

//Exectute The cURL
$result = curl_exec($ch);

//Close Up The Connection
curl_close($ch);    


?>

You need to urlencode your $params wherever there may be @, whitespaces and anything that cannot be in url string:

$params = array(
    "client_id" => $client_id,
    "response_type" => "code",
    "state" => $random_state,
    "redirect_uri" => urlencode($redirect_uri),
    "duration" => "permanent",
    "scope" => "submit"
);

AND make it a string:

$params = http_build_query($params);

Otherwise your post will be corrupted.

Also, as you connect with ssl, you should add these lines:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);