PHP CURL 412错误

时间:2016-02-15 21:01:54

标签: php curl http-status-code-412

我正在使用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());
?>

但它不起作用。 我该如何解决?

2 个答案:

答案 0 :(得分:1)

通常,如果您与进行身份验证的网站进行交互,则需要cURL的cookiejar参数来保存会话信息。如果您不将会话信息发送回主机,则很可能会导致您的注册出现问题。

Here's a class I use to authenticate users remotely via cURL.

/* Makes an HTTP request
 * @param String $url - The URL to request
 * @param Mixed $params - string or array to POST
 * @param String - filename to download
 */
public static function request($url, $params = array(), $filename = "") {

    // Initiate cURL
    $ch = curl_init();
    $curlOpts = array(
        CURLOPT_URL => $url,
        CURLOPT_USERAGENT => 
            'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true
    );

    // Send the cookies if we're logged in
    if (!empty(self::$cookiejar)) {
        $curlOpts[CURLOPT_COOKIEJAR] = self::$cookiejar;
        $curlOpts[CURLOPT_COOKIEFILE] = self::$cookiejar;
    }

    // If $filename exists, save content to file
    if (!empty($filename)) {
        $file2 = fopen($filename, 'w+') or die("Error[" . __FILE__ . ":" . __LINE__ . "] Could not open file: $filename");
        $curlOpts[CURLOPT_FILE] = $file2;
    }
    // Send POST values if there are any
    if (!empty($params)) {
        $curlOpts[CURLOPT_POST] = true;
        $curlOpts[CURLOPT_POSTFIELDS] = is_array($params) ? 
            http_build_query($params) : $params;
    }

    // Send the request
    curl_setopt_array($ch, $curlOpts);
    $answer = curl_exec($ch);

    // Errors?
    if (curl_error($ch)) die($url . " || " . curl_error($ch));

    // Close connection and return response
    curl_close($ch);
    if(!empty($filename)) fclose($file2);
    return $answer;
}

答案 1 :(得分:0)

1:curl_setopt($ch, CURLOPT_HTTPHEADER, array());
需要$curl而不是$ch

2:curl_setopt($curl, CURLOPT_POST, "5");
不要期望5,它需要TRUEFALSE,请参阅curlopt_post (php.net)

2.1:CURLOPT_FOLLOWLOCATION还需要TRUEFALSE

编辑:我的错误,1和0也是布尔