我正在尝试使用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(前提条件失败)。
(来源: 什么时候响应 HTTP 412 错误是合适的?)
所以我添加了这些标题
<?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());
?>
但它不起作用。我该如何解决?