我试图通过curl发送请求来休息api,但我总是有404响应。当我通过PhpStorm执行相同的请求时,我的请求正常工作。我在rest客户端中设置与curl中相同的头文件。我做错了什么?下面是我的PHP代码和其他客户端配置的截图
$postfields = array(
'SUBSCRIPTION' => 'testlist',
"HTTP_REDIRECT" => 'https://testsite.com',
"HTTP_REDIRECT_ERROR" => 'https://testsite.com/error',
"email" => 'test+100@gmail.com',
"Sprache" => 'DE'
);
$uri = $uri . '?=' . http_build_query($postfields); // value of uri is http://web.mailingsystem.com/subscription/servlet?=SUBSCRIPTION=testlist&HTTP_REDIRECT=https%3A%2F%2Ftestsite.com%2F&HTTP_REDIRECT_ERROR=https%3A%2F%2Ftestsite.com%2Ferror&email=test%2B100A%40gmail.com&Sprache=DE
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;from testshop;os:".PHP_OS.";)");
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$headers = [
'Content-Length:'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$content = curl_exec($ch);
$error = curl_error($ch);
$response = curl_getinfo($ch);
curl_close($ch);
答案 0 :(得分:0)
首先,你发送一个不完整的"内容长度"头。最有可能的是,目标服务器将其视为" content-length:0",但您应该联系api的开发人员并告诉他们修复它,因此它会返回400 Bad Request
响应,而不是一个404 Not Found
响应,那么您应该修复您的代码,因此它具有正确的内容长度标头。最好的方法是不要插入一个内容长度的标题,这使得curl为你生成正确的标题(当你有一个请求体时)
第二,你的RestClient似乎正在使用multipart/form-data
编码,但你的curl代码根本没有请求体,数据在GET url中,修复它,所以curl与RestClient匹配与CURLOPT_POSTFIELDS一样,curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($postfields));
,并从网址中删除数据。它可能是因为您的网址格式错误(包含帖子数据正文),导致您收到404错误