POST works on Postman, but not with CURL

Using the postman, I send POST (my username and password are filled in):

https://ssl.reddit.com/api/login?api_type=json&user=XXX&passwd=XXX&rem=True 

I get a response containing modhash and cookie. Then I send a second POST with a postman to:

https://en.reddit.com/api/comment?api_type=json&text=7/1/15TEST&thing_id=t1_csa56v2

with the following headings (XXX verified and completed):

User-Agent: XXX
Cookie: reddit_session=XXX
X-Modhash: XXX

This provides the correct answer, but when I try to do the same with CURL in my PHP, it answers USER_REQUIRED. Once again, I confirmed that the cookies 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 answer?

POSTMAN Screenshot: Screenshot of POSTMAN

+4
source share
1 answer
<?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);

. , , CURLOPT_USERAGENT, cookie CURLOPT_COOKIE, , http_build_query , POST, GET . E_ALL

+3

All Articles