【问题标题】:PHP CURL 412 errorPHP CURL 412 错误
【发布时间】: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


【解决方案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;
}

【讨论】:

    【解决方案2】:

    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 也是布尔值

    【讨论】:

    • 0 和 1 是转换为布尔值的整数。我相信 5 也可以算作true
    猜你喜欢
    • 2019-01-01
    • 2017-01-12
    • 1970-01-01
    • 2018-07-12
    • 2013-02-03
    • 2013-12-01
    • 2012-01-23
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多