Skip to content
Snippets Groups Projects
Commit d5e5f762 authored by Dave Reid's avatar Dave Reid
Browse files

Added a HttpHelper for caching drupal_http_requests().

parent 93522bf7
No related branches found
Tags 9.3.3
No related merge requests found
......@@ -7,6 +7,7 @@ files[] = lib/CacheHelper.php
files[] = lib/EntityHelper.php
files[] = lib/FeaturesHelper.php
files[] = lib/FieldHelper.php
files[] = lib/HttpHelper.php
files[] = lib/RenderHelper.php
files[] = includes/HelperDebugMailLog.php
files[] = views/views_handler_field_helper_entity_operation_links.inc
<?php
class HttpHelper {
public static function cachedRequest($url, array $options = array(), $cache_errors = FALSE) {
$cid = static::cachedRequestGetCid($url, $options);
$bin = isset($options['cache']['bin']) ? $options['cache']['bin'] : 'cache';
if ($cid && $cache = CacheHelper::get($cid, $bin)) {
return $cache->data;
}
else {
$response = drupal_http_request($url, $options);
if (!$cache_errors && !empty($response->error)) {
$cid = FALSE;
}
if ($cid) {
$expire = isset($options['cache']['expire']) ? $options['cache']['expire'] : CACHE_TEMPORARY;
cache_set($cid, $response, $bin, $expire);
}
return $response;
}
}
public static function cachedRequestGetCid($url, array $options) {
if (isset($options['cache']) && $options['cache'] === FALSE) {
return FALSE;
}
if (isset($options['cache']['cid'])) {
return $options['cache']['cid'];
}
$cid_parts = array($url, serialize(array_diff_key($options, drupal_map_assoc(array('cache')))));
return 'http-request:' . drupal_hash_base64(serialize($cid_parts));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment