Using postman, I send a POST to (my username and password are filled in):
https://ssl.reddit.com/api/login?api_type=json&user=XXX&passwd=XXX&rem=True
I receive a response containing a modhash and a cookie. Then, I send a second POST with postman to:
https://en.reddit.com/api/comment?api_type=json&text=7/1/15TEST&thing_id=t1_csa56v2
with the following headers (XXX has been confirmed and filled in):
User-Agent: XXX
Cookie: reddit_session=XXX
X-Modhash: XXX
This provides the correct response, but when I try to do the same thing with CURL in my PHP, it responds with USER_REQUIRED. Once again, I have confirmed that the cookie and modhash are correct.
$name = 't1_csa56v2';
$text = 'NEWEST TEST 7/2/15 12:20am';
$url = 'https://en.reddit.com/api/comment';
$modhash = 'XXX';
$cookie = 'XXX';
$headerFields = array (
'User-Agent' => 'XXX',
'Cookie' => 'reddit_session='.$cookie,
'X-Modhash' => $modhash
);
$postFields = array (
'api_type' => 'json',
'text' => $text,
'thing_id' => $name
);
$field_string = http_build_query($postFields);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 3);
curl_setopt($ch, CURLOPT_POSTFIELDS, $field_string);
$response = curl_exec($ch);
What am I doing wrong? Why can't I get the same response?
Screenshot of POSTMAN:
<?php
error_reporting(E_ALL);
$name = 't1_csa56v2';
$text = 'NEWEST TEST 7/2/15 12:20am';
$url = 'https://en.reddit.com/api/comment';
$modhash = 'XXX';
$cookie = 'XXX';
$headerFields = array (
'X-Modhash' => $modhash
);
$postFields = array (
'api_type' => 'json',
'text' => $text,
'thing_id' => $name
);
$ch = curl_init($url);
assert(curl_setopt_array($ch,
array(
CURLOPT_AUTOREFERER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FORBID_REUSE => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 11,
CURLOPT_ENCODING=>"",
CURLOPT_USERAGENT=>'XXX',
CURLOPT_COOKIE=>'reddit_session='.$cookie,
CURLOPT_HTTPHEADER=>$headerFields,
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$postFields,
)));
$response = curl_exec($ch);
try this.. not sure exactly what you do wrong, but user agent should be set with CURLOPT_USERAGENT , and the cookie should be set with CURLOPT_COOKIE and you should let curl encode it for you, rather than using http_build_query , and you should explicitly set it to a POST request, as its a GET request by default. should also enable E_ALL error reporting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With