Friday, 15 March 2013

PHP & cURL Not Working -


A reddit bot building.

Ignore the actual API for each other - it does not handle curl even to execute - I can not see any requests / reactions in browsers DevTools.

  & lt ;? Php / ** * Reddit PHP WikiBot * * @ author tomgs * @ link mysite.com * / / ** * Authentication Using OAuth 2.0 - Step 1 * * Get the code, state & amp; Reading token reddit * @ link @ https://github.com/reddit/reddit/wiki/OAuth2#authorization * * // start curl $ ch = curl_init (); / * Set the curl wars * / // URLs to the code, state and amp; in order to obtain; $ AuthorizeUrl = 'https: //www.reddit.com/api/v1/authorize' token; Curl_setopt ($ ch, CURLOPT_URL, $ authorizeUrl); // If curl phelps die & amp; Throw an error / curl_setopt ($ ch, CURLOPT_FAILONERROR, TRUE); // include the header in the output curl_setopt ($ CH, CURLOPT_HEADER, TRUE); // a post request defaulted GET request curl_setopt ($ CH, CURLOPT_POST, 6) endued; // Return return as a string of value return; Curl_setopt ($ CH, CURLOPT_RETURNTRANSFER, TRUE); / ** * Parameter for post request - explained * * @ Ultimate string $ client_id You set the app's Client ID * @ Param String to $ response_type "code" - For this step, @ @ the absolute string $ state is selected by me A random string * @trust string $ debutory set to permanent, because we want to get the refresh token from the server (more about later refresh token) * @truth_trag The authentication was successful to not * * * * / / / slightly longer / more complex string - $ client_id = 'CLIENTID' stored in a local variable; $ Redirect_uri = "http: //localhost/redditbot2/callback.php"; $ Random_state = RAND (); $ Params = array ("client_id" => $ client_id, "response_type" = & gt; "code", "state" => $ random_state, "redirect_uri" = & gt; urlencode ($ redirect_uri), " Period "= & gt;" permanent "," scope "= & gt;" submit "); $ Params = http_build_query ($ params); // Set all fields for post request curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params); // Exectute curl $ result = curl_exec ($ ch); // Close the connection curl_close ($ ch); ? & Gt;  

You may need your $ params urlencode @ should be white space and anything that is not in the url string:

  $ params = array ("client_id" => $ client_id, "response_type" = & Gt; = "Gtc:  

and make this a string:

  $ params = http_build_query ($ params);   

In addition, as you connect to SSL, you should add these rows:

  curl_setopt ($ CH, CURLOPT_SSL_VERIFYHOST, 0); Curl_setopt ($ CH, CURLOPT_ SSL_VERIFYPEER, 0);  

No comments:

Post a Comment