簡體   English   中英

OAuth,PHP,Rest API和curl發出400錯誤請求

[英]OAuth, PHP, Rest API and curl gives 400 Bad Request

我們使用了帶有OAuth 1.0的car2go Rest-API的幾個應用程序。

我們所有的網絡應用都在2天前停止工作。 現在,所有curl POST請求都失敗,並顯示以下錯誤:

400 Bad Request
Your browser sent a request that this server could not understand.
Error code: 53
Parser Error: [Content-Length: -]

我花費大量時間試圖找出問題是否出在我的oauth工作流程上。 但是最后所有參數,簽名和內容都是正確的。 我通過郵遞員 (REST客戶端)成功觸發了POST

所以我的結論是,curl的php代碼突然不再起作用了。

這是(非常難看)卷曲功能。 與大多數有關curl POST的教程不同的是,我傳遞的是已附加所有參數的完整URL,因此不需要CURLOPT_POSTFIELDS

function curlAPI($params) {

    //open connection
    $ch = curl_init();

    $url = $params['url'];


    curl_setopt($ch,CURLOPT_HEADER,false);

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);


    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch, CURLOPT_MAXREDIRS,50);

    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);

    if($params['type'] == 'POST') {
        // POST
        curl_setopt($ch,CURLOPT_POST, true);
    } else if($params['type'] == 'DELETE') {
        // DELETE
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    } else if($params['type'] == 'PUT') {
        $update_json = array();
        // PUT
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS,'');
    } else {
        // GET
        curl_setopt($ch,CURLOPT_POST,0);
    }


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    //execute post
    $result['result'] = curl_exec($ch);

    // debug
    if (FALSE === $result['result']) {
        $result['errorInfo'] = curl_error($ch).' - '.curl_errno($ch);
    }

    $reponseInfo = array();

    $reponseInfo['info'] = curl_getinfo($ch);
    $reponseInfo['error'] = curl_error($ch);

    //close connection
    curl_close($ch);


    $result['reponseInfo'] = $reponseInfo;
    return json_encode($result);

}

好的,這是解決此噩夢的方法:

curl_setopt($ch,CURLOPT_HTTPHEADER ,array('Content-Length: 0'));

另外,發送沒有標題的curl POST也不可行。

暫無
暫無

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

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