簡體   English   中英

POST適用於Postman,但不適用於CURL

[英]POST works on Postman, but not with CURL

使用郵遞員,我發送一個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的屏幕截圖: 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);

試試這個..不確定你做錯了什么,但是應該用CURLOPT_USERAGENT設置用戶代理,並且應該用CURLOPT_COOKIE設置cookie,你應該讓curl為你編碼,而不是使用http_build_query,你應該明確地設置它到POST請求,默認為GET請求。 還應該啟用E_ALL錯誤報告

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM