1
votes

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);

}
1
If it was working before and you changed nothing, then maybe Car2Go is blocking you? - ItalyPaleAle
Have you tried running the curl command in commandline (not through PHP), did any changes happen to any config of PHP/Curl/...these last days? - Tom
@Qualcuno how could I check if they've blocked me (besides asking)? GET and DELETE requests are still working, - ProblemsOfSumit
@Tom haven't tried from the command line (because I don't know how). But I'll ask our server provider if changes happened to PHP/Curl lately. - ProblemsOfSumit
Parser Error: [Content-Length: -] – that sounds like your call might not include a proper Content-Length header. To debug this, I’d suggest you change the URL you’re making the cURL request to to a script of your own, that simply logs the HTTP request headers and received POST content to a file … and then check if that looks proper or not. - CBroe

1 Answers

2
votes

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.