0

I'm triying to register on a website using PHP CURL. Everything is okay, but when I execute my code I get an error from the host:

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 )

After doing some research on this website, I've found this:

If you look at RFC 2616 you'll see a number of request headers that can be used to apply conditions to a request:

If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since These headers contain 'preconditions', allowing the client to tell the server to only complete the request if certain conditions are met. For example, you use a PUT request to update the state of a resource, but you only want the PUT to be actioned if the resource has not been modified by someone else since your most recent GET.

The response status code 412 (Precondition Failed) is typically used when these preconditions fail.

(source: When is it appropriate to respond with a HTTP 412 error?)

So I've added these headers

<?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());
?>

but It doesn't work. How can I solve it?

Community
  • 1
  • 1
  • If those are real credentials, you should delete them and flag this post for a moderator to redact them. – elixenide Feb 15 '16 at 21:07
  • These are random-typed false creditentials, but I can remove them if you want –  Feb 15 '16 at 21:08
  • No need. People post their real credentials here all the time, so I just wanted to alert you in case you had made that mistake. – elixenide Feb 15 '16 at 21:08
  • I alerdy removed them, but thanks for the alert –  Feb 15 '16 at 21:09
  • Remove all the "If-*" headers as they're all malformed anyway and will only cause more trouble. Remove the Content-Length too, curl will add that by itself. The Connection: one isn't necessary. You're not using CURLOPT_RETURNTRANSFER so you won't get the response returned. – Daniel Stenberg Feb 15 '16 at 21:25

2 Answers2

1

Generally, if you're interacting with a website that does authentication, you will need the cookiejar parameters for cURL to save session info. If you don't send session info back to the host it will most likely cause problems with your registration.

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;
}
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
0

1: curl_setopt($ch, CURLOPT_HTTPHEADER, array());
needs $curl instead of $ch.

2: curl_setopt($curl, CURLOPT_POST, "5");
doesn't expect 5, it needs TRUE or FALSE, see curlopt_post (php.net).

2.1: CURLOPT_FOLLOWLOCATION also expects TRUE or FALSE.

Edit: My mistake, 1 and 0 are booleans too

Tom Udding
  • 2,264
  • 3
  • 20
  • 30