You are here

private static function ShareaholicHttp::send_with_drupal in Share Buttons, Related Posts, Content Analytics - Shareaholic 8

Same name and namespace in other branches
  1. 7.3 lib/social-share-counts/drupal_http.php \ShareaholicHttp::send_with_drupal()
1 call to ShareaholicHttp::send_with_drupal()
ShareaholicHttp::send in lib/social-share-counts/drupal_http.php
Performs a HTTP request with a url, array of options, and ignore_error flag

File

lib/social-share-counts/drupal_http.php, line 56

Class

ShareaholicHttp
The purpose of this class is to provide an interface around any native http function (wp_remote_get, drupal_http_request, curl) so that one use this consistent API for making http request with well defined input and output.

Code

private static function send_with_drupal($url, $options, $ignore_error) {
  $request = array();
  $result = array();
  $request['method'] = isset($options['method']) ? $options['method'] : 'GET';
  $request['headers'] = isset($options['headers']) ? $options['headers'] : array();
  $request['max_redirects'] = isset($options['redirection']) ? $options['redirection'] : 5;
  $request['timeout'] = isset($options['timeout']) ? $options['timeout'] : 15;
  $request['headers']['User-Agent'] = isset($options['user-agent']) ? $options['user-agent'] : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0';
  if (isset($options['body'])) {
    if (isset($request['headers']['Content-Type']) && $request['headers']['Content-Type'] === 'application/json') {
      $request['data'] = json_encode($options['body']);
    }
    else {
      $request['data'] = http_build_query($options['body']);
    }
  }
  else {
    $request['body'] = NULL;
  }
  $response = drupal_http_request($url, $request);
  if (isset($response->error)) {
    if (!$ignore_error) {
      ShareaholicUtilities::log('ShareaholicHttp Error for ' . $url . ' with error ' . $response->error);
    }
    return false;
  }
  $result['headers'] = $response->headers;
  $result['body'] = $response->data;
  $result['response'] = array(
    'code' => $response->code,
    'message' => $response->status_message,
  );
  return $result;
}