OAuth的,PHP,REST API和卷曲给出400错误的请求 [英] OAuth, PHP, Rest API and curl gives 400 Bad Request

查看:139
本文介绍了OAuth的,PHP,REST API和卷曲给出400错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一对夫妇的应用程序,利用的car2go的REST API 使用OAuth 1.0。

我们所有的Web应用3天前停止工作。所有卷曲 POST 的要求,现在是失败,出现以下错误:

  400错误的请求
您的浏览器发送该服务器无法理解的请求。
错误code:53
分析器错误:[内容长度: - ]
 

我花了很多时间试图找出问题是否与我的OAuth的认证流程。但最终所有的参数和签名之类的东西是正确的。我成功地火了 POST 通过<一href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm"相对=nofollow>邮差(REST客户端)

所以,我的结论是,不知怎的,PHP的$ C $下卷曲突然不工作了。

这是(非常丑陋)卷曲功能。的差异约卷曲POST大多数教程是,我传递一个完整的URL与已连接的所有参数,所以我不需要 CURLOPT_POSTFIELDS

 函数curlAPI($ params)方法{

    //打开连接
    $ CH = curl_init();

    $ URL = $ PARAMS ['URL'];


    curl_setopt($ CH,CURLOPT_HEADER,假);

    //设置URL,POST数瓦尔,POST数据
    curl_setopt($ CH,CURLOPT_URL,$网址);


    curl_setopt($ CH,CURLOPT_USERAGENT,$ _ SERVER ['HTTP_USER_AGENT']);
    curl_setopt($ CH,CURLOPT_FOLLOWLOCATION,真正的);
    curl_setopt($沟道,CURLOPT_MAXREDIRS,50);

    curl_setopt($ CH,CURLOPT_TIMEOUT_MS,5000);

    如果($ PARAMS ['类型'] =='POST'){
        // POST
        curl_setopt($ CH,CURLOPT_POST,真正的);
    }否则,如果($ PARAMS ['类型'] =='删除'){
        // 删除
        curl_setopt($沟道,CURLOPT_CUSTOMREQUEST,DELETE);
    }否则,如果($ PARAMS ['类型'] =='把'){
        $ update_json =阵列();
        // 放
        curl_setopt($ CH,CURLOPT_CUSTOMREQUEST,PUT);
        curl_setopt($沟道,CURLOPT_POSTFIELDS,'');
    } 其他 {
        // 得到
        curl_setopt($沟道,CURLOPT_POST,0);
    }


    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);

    curl_setopt($ CH,CURLOPT_FOLLOWLOCATION,真正的);

    //实行岗位
    $结果['结果'] = curl_exec($ CH);

    //调试
    如果(FALSE === $结果['结果']){
        $结果['errorInfo中'] = curl_error($ CH)。 - .curl_errno($ CH);
    }

    $ reponseInfo =阵列();

    $ reponseInfo ['信息'] = curl_getinfo($ CH);
    $ reponseInfo ['错误'] = curl_error($ CH);

    //关闭连接
    curl_close($ CH);


    $结果['reponseInfo'] = $ reponseInfo;
    返回json_en code($结果);

}
 

解决方案

好了,这就是解决了这个噩梦:

  curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列(内容长度:0'));
 

Aparrently这是不正常发送卷曲 POST 没有头。

We got a couple of apps, utilizing the car2go Rest-API with OAuth 1.0.

All our web apps stopped working 2 days ago. All curl POST requests are failing now with the following error:

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

I spend a lot of time trying to figure out if the problem is my oauth workflow. But in the end all parameters and signatures and stuff is correct. I successfully fire the POST via Postman (REST-Client)

So my conclusion is that somehow the php code for the curl is suddenly not working anymore.

This is the (very ugly) curl function. A difference to most tutorials about curl POST is, that I'm passing a full URL with all parameters already attached, so I don't need 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);

}

解决方案

Ok, this is what fixed this nightmare:

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

Aparrently it's not ok to send a curl POST without a header.

这篇关于OAuth的,PHP,REST API和卷曲给出400错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆