douxiong5972 2015-03-16 17:04
浏览 59
已采纳

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


?>
  • 写回答

1条回答 默认 最新

  • duaevb1511 2015-03-16 17:14
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程