0

を使用して Web サイトに登録しようとしてい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 要求を使用しますが、リソースが最新の GET 以降に他のユーザーによって変更されていない場合にのみ、PUT を実行する必要があります。

応答ステータス コード 412 (Precondition Failed) は、通常、これらの前提条件が失敗した場合に使用されます。

(ソース: 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());
?>

しかし、うまくいきません。どうすれば解決できますか?

4

2 に答える 2