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 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟