php - POST works on Postman, but not with CURL -
using postman, send post (my username , password filled in):
https://ssl.reddit.com/api/login?api_type=json&user=xxx&passwd=xxx&rem=true
i receive response containing modhash , cookie. then, send second post postman to:
https://en.reddit.com/api/comment?api_type=json&text=7/1/15test&thing_id=t1_csa56v2
with following headers (xxx has been confirmed , filled in):
user-agent: xxx cookie: reddit_session=xxx x-modhash: xxx
this provides correct response, when try same thing curl in php, responds user_required. once again, have confirmed cookie , modhash 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 doing wrong? why can't 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 wrong, user agent should set curlopt_useragent , , cookie should set curlopt_cookie , should let curl encode you, rather using http_build_query , , should explicitly set post request, request default. should enable e_all error reporting
Comments
Post a Comment