1<?php
2
3namespace ReCaptcha\RequestMethod;
4
5use ReCaptcha\RequestMethod;
6use ReCaptcha\RequestParameters;
7
8/**
9 * Sends cURL request to the reCAPTCHA service.
10 */
11class Curl implements RequestMethod
12{
13    /**
14     * URL to which requests are sent via cURL.
15     * @const string
16     */
17    const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
18
19    /**
20     * Submit the cURL request with the specified parameters.
21     *
22     * @param RequestParameters $params Request parameters
23     * @return string Body of the reCAPTCHA response
24     */
25    public function submit(RequestParameters $params)
26    {
27        $handle = curl_init(self::SITE_VERIFY_URL);
28
29        $options = array(
30            CURLOPT_POST => true,
31            CURLOPT_POSTFIELDS => $params->toQueryString(),
32            CURLOPT_HTTPHEADER => array(
33                'Content-Type: application/x-www-form-urlencoded'
34            ),
35            CURLINFO_HEADER_OUT => false,
36            CURLOPT_HEADER => false,
37            CURLOPT_RETURNTRANSFER => true,
38            CURLOPT_SSL_VERIFYPEER => true
39        );
40        curl_setopt_array($handle, $options);
41
42        $response = curl_exec($handle);
43        curl_close($handle);
44
45        return $response;
46    }
47}
48