Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php curl在reddit上提交链接时出错_Php_Curl_Reddit - Fatal编程技术网

使用php curl在reddit上提交链接时出错

使用php curl在reddit上提交链接时出错,php,curl,reddit,Php,Curl,Reddit,我正在尝试使用php curl在reddit.com上提交链接帖子。我有用户的访问令牌,但我无法在reddit上发布,并获得错误代码403。下面是我试图执行的示例代码: $subreddit='funny'; $kind='link'; $postVals = sprintf("kind=%s&sr=%s&title=%s&r=%s", $kind, $subreddit, urlencode('T

我正在尝试使用php curl在reddit.com上提交链接帖子。我有用户的访问令牌,但我无法在reddit上发布,并获得错误代码403。下面是我试图执行的示例代码:

$subreddit='funny';
$kind='link';

$postVals = sprintf("kind=%s&sr=%s&title=%s&r=%s",
            $kind,
            $subreddit,
            urlencode('Test Post'),
            $subreddit);
$postVals .= "&url=" . urlencode( 'http://seextra.com');

$urlSubmit = "http://www.reddit.com/api/v1/submit";
$ch = curl_init($urlSubmit);
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 10
);

if (!empty($_SERVER['HTTP_USER_AGENT'])){
    $options[CURLOPT_USERAGENT] =      $_SERVER['HTTP_USER_AGENT'];
}

if ($postVals != null){
    $options[CURLOPT_POSTFIELDS] = $postVals;
    $options[CURLOPT_CUSTOMREQUEST] = "POST";
}

$headers = array("Authorization:bearer".$access_token , "User-Agent:".$_SERVER['HTTP_USER_AGENT']);
$options[CURLOPT_HEADER] = false;
$options[CURLINFO_HEADER_OUT] = false;
$options[CURLOPT_HTTPHEADER] = $headers;
$options[CURLOPT_USERPWD] = Reddit_Client_Id . ":" . Reddit_Client_secret; 

// Reddit_Client_Id client id
// Reddit_Client_secret secret key
$options[CURLOPT_SSLVERSION] = 4;
$options[CURLOPT_SSL_VERIFYPEER] = false;
$options[CURLOPT_SSL_VERIFYHOST] = 2;
pr($options);

curl_setopt_array($ch, $options);
$apiResponse = curl_exec($ch);
$response = json_decode($apiResponse);
curl_close($ch);
print_r($response);
以下是reddit的回应:

stdClass Object
(
    [error] => 403
)
你能帮我解决这个问题吗

谢谢,
Sachin

您确定您有权访问适当的资源,并且您的访问令牌是正确的吗?是的,因为每次我尝试使用CURL via POST方法连接reddit.com时,我都会获得用户的访问令牌和刷新令牌。以下是我用于授权的post字段$字段=数组(“授权类型”=>“授权代码”,“代码”=>$\u获取[“代码”],“重定向uri”=>Reddit\u重定向uri);下面是我得到的访问令牌响应:stdClass对象([access\u token]=>/*access\u token*/[token\u type]=>bearer[expires\u in]=>3600[refresh\u token]=>/*refresh token*/[scope]=>identity submit)