【发布时间】:2015-04-28 23:46:40
【问题描述】:
我们有几个应用程序,使用带有 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 工作流程上。但最后所有参数和签名和东西都是正确的。我通过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);
}
【问题讨论】:
-
如果它之前可以正常工作而您没有进行任何更改,那么可能 Car2Go 阻止了您?
-
您是否尝试过在命令行中运行 curl 命令(而不是通过 PHP),最近几天 PHP/Curl/...的任何配置是否发生了任何变化?
-
@Qualcuno 我如何检查他们是否阻止了我(除了询问)?
GET和DELETE请求仍然有效, -
@Tom 没有从命令行尝试过(因为我不知道如何)。但是我会询问我们的服务器提供商,如果 PHP/Curl 最近发生了变化。
-
Parser Error: [Content-Length: -]- 听起来您的电话可能不包含正确的Content-Length标头。要对此进行调试,我建议您将发出 cURL 请求的 URL 更改为您自己的脚本,该脚本仅记录 HTTP 请求标头并将接收到的 POST 内容写入文件……然后检查这是否正确或不是。