OAuth、PHP、Rest API 和 curl 给出 400 Bad Request
OAuth, PHP, Rest API and curl gives 400 Bad Request
我们有几个应用程序,使用 car2go Rest-API 和 OAuth 1.0。
我们所有的网络应用程序在 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 工作流程上。但最后所有的参数和签名都是正确的。我通过 Postman (REST-Client)
成功触发了 POST
所以我的结论是,curl 的 php 代码突然失效了。
这是(非常丑陋的)curl 函数。与大多数关于 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'));
显然,在没有 header 的情况下发送 curl POST
是不正确的。
我们有几个应用程序,使用 car2go Rest-API 和 OAuth 1.0。
我们所有的网络应用程序在 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 工作流程上。但最后所有的参数和签名都是正确的。我通过 Postman (REST-Client)
成功触发了POST
所以我的结论是,curl 的 php 代码突然失效了。
这是(非常丑陋的)curl 函数。与大多数关于 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'));
显然,在没有 header 的情况下发送 curl POST
是不正确的。