remote::request PHP Method

request() public method

public request ( $url, $post = false, $options = [] )
    function request($url, $post = false, $options = array())
    {
        $defaults = array('timeout' => 10, 'headers' => array(), 'encoding' => 'utf-8', 'agent' => self::agent());
        $options = array_merge($defaults, $options);
        $ch = curl_init();
        $params = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => $options['encoding'], CURLOPT_USERAGENT => $options['agent'], CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => $options['timeout'], CURLOPT_TIMEOUT => $options['timeout'], CURLOPT_MAXREDIRS => 10, CURLOPT_SSL_VERIFYPEER => false);
        if (!empty($options['headers'])) {
            $params[CURLOPT_HTTPHEADER] = $options['headers'];
        }
        if (!empty($post)) {
            $params[CURLOPT_POST] = true;
            $params[CURLOPT_POSTFIELDS] = $post;
        }
        curl_setopt_array($ch, $params);
        $content = curl_exec($ch);
        $error = curl_errno($ch);
        $message = curl_error($ch);
        $response = curl_getinfo($ch);
        curl_close($ch);
        $response['error'] = $error;
        $response['message'] = $message;
        $response['content'] = $content;
        self::$response = $response;
        if (a::get($response, 'error')) {
            return array('status' => 'error', 'msg' => 'The remote request failed: ' . $response['message']);
        }
        if (a::get($response, 'http_code') >= 400) {
            return array('status' => 'error', 'msg' => 'The remote request failed - code: ' . $response['http_code'], 'code' => $response['http_code']);
        }
        return $response;
    }

Usage Example

Example #1
0
        foreach ($only as $key) {
            $data[$key] = $form[$key];
        }
    } else {
        $data = $form;
        // remove those fields specified in 'except'
        foreach (a::get($actionOptions, 'except', array()) as $key) {
            unset($data[$key]);
        }
    }
    $params = a::get($actionOptions, 'params', array());
    // merge the optional 'static' data from the action array with the form data
    $params['data'] = array_merge(a::get($params, 'data', array()), $data);
    $headers = array('Content-Type: application/x-www-form-urlencoded');
    $params['headers'] = array_merge(a::get($params, 'headers', array()), $headers);
    $response = remote::request($url, $params);
    if ($response->error === 0) {
        return array('success' => true, 'message' => l::get('uniform-webhook-success'));
    } else {
        return array('success' => false, 'message' => l::get('uniform-webhook-error') . $response->message);
    }
};
/*
 * Action to choose from multiple recipients who should receive the form by
 * email.
 */
uniform::$actions['email-select'] = function ($form, $actionOptions) {
    $allowed = a::get($actionOptions, 'allowed-recipients');
    if (!is_array($allowed)) {
        throw new Exception('Uniform email select action: No allowed recipients!');
    }