【发布时间】:2016-05-26 22:22:13
【问题描述】:
我正在尝试使用PHP CURL在网站上注册。一切都好的,但是当我执行我的代码时,我从主机收到一个错误:
HTTP/1.1 412 Precondition Failed
Date: Mon, 15 Feb 2016 20:54:58 GMT
Server: Varnish
X-Varnish: 317635174
Content-Length: 0
Array ( [header] => 1 [body] => [res] => 1 )
在对这个网站做了一些研究之后,我发现了这个:
如果您查看 RFC 2616,您会看到许多请求标头 可用于对请求应用条件:
If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since这些标头包含“先决条件”,允许客户端告诉 服务器仅在满足某些条件时才完成请求。为了 例如,您使用 PUT 请求来更新资源的状态,但是 如果资源尚未被执行,您只希望 PUT 被执行 自您最近一次 GET 以来被其他人修改。通常使用响应状态码 412(前提条件失败) 当这些前提条件失败时。
(来源:When is it appropriate to respond with a HTTP 412 error?)
所以我添加了这些标题
<?php
function register() {
$curl = curl_init();
$post = "name=username&email=".urlencode("email@email.com")."&password=thepassword&repassword=thepassword&parrain=test";
$useragent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36';
curl_setopt($curl, CURLOPT_URL, '[the website]');
curl_setopt($curl, CURLOPT_POST, "5");
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Connection: keep-alive",
"Content-Length: 43",
"Cache-Control: max-age=0",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Upgrade-Insecure-Requests: 1",
"Content-Type: application/x-www-form-urlencoded",
"Accept-Encoding: gzip, deflate",
"Accept-Language: fr,fr-FR;q=0.8,en;q=0.6,en-US;q=0.",
"If-Match: 1",
"If-Modified-Since: 1",
"If-None-Match: 1",
"If-Range: 1",
"If-Unmodified-Since: 1"
));
curl_setopt($curl, CURLOPT_HEADER, true);
$result = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($curl);
return array(
"header" => $header,
"body" => $body,
"res" => $result
);
}
print_r(register());
?>
但它不起作用。 我该如何解决?
【问题讨论】:
-
如果这些是真实凭据,您应该删除它们并标记此帖子以供版主编辑。
-
这些是随机输入的虚假信用,但如果你愿意,我可以删除它们
-
不需要。人们一直在这里发布他们的真实凭据,所以我只是想提醒你,以防你犯了这个错误。
-
我已经删除了它们,但感谢提醒
-
删除所有“If-*”标头,因为它们都是格式错误的,只会造成更多麻烦。也删除 Content-Length,curl 会自行添加。连接:一个不是必需的。你没有使用 CURLOPT_RETURNTRANSFER 所以你不会得到返回的响应。
标签: php curl http-status-code-412