使用邮递员,我发送一个POST(我的用户名和密码已填写):
https://ssl.reddit.com/api/login?api_type=json&user=XXX&passwd=XXX&rem=True
我收到包含modhash和Cookie的回复。然后,我发送第二个POST邮递员到:
https://en.reddit.com/api/comment?api_type=json&text=7/1/15TEST&thing_id=t1_csa56v2
带有以下标题的(XXX已经确认并填写):
User-Agent: XXX
Cookie: reddit_session=XXX
X-Modhash: XXX
这提供了正确的响应,但是当我尝试在PHP中使用CURL执行相同的操作时,它会以USER_REQUIRED响应。我再一次确认cookie和modhash是正确的。
$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);
我做错了什么?为什么我不能得到相同的回复?
POSTMAN的屏幕截图:
答案 0 :(得分:3)
<?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设置用户代理,并且应该用CURLOPT_COOKIE设置cookie,你应该让curl为你编码,而不是使用http_build_query ,您应该将其显式设置为POST请求,默认为GET请求。还应该启用E_ALL错误报告