0

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

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

4

2 回答 2

1

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

这是我用来通过 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;
}
于 2016-02-15T21:22:30.353 回答
0

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

2:curl_setopt($curl, CURLOPT_POST, "5");
不要指望5,它需要TRUEor FALSE,见curlopt_post (php.net)

2.1:CURLOPT_FOLLOWLOCATION也期望TRUEor FALSE

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

于 2016-02-15T21:21:28.413 回答