示例#1
0
 /**
  * test the helper function to add params to a url
  */
 public function test_http_add_params()
 {
     $new_url = http_add_params('http://www.whereivebeen.com', 'new=params');
     $this->assertEquals('http://www.whereivebeen.com?new=params', $new_url);
     $new_url = http_add_params('http://ad.doubleclick.net/clk;222748503;44958519;r?http://www.monogramstravel.com', 'new=params');
     $this->assertEquals('http://ad.doubleclick.net/clk;222748503;44958519;r?http%3A%2F%2Fwww.monogramstravel.com=&new=params', $new_url);
 }
示例#2
0
文件: wib.php 项目: Tapac/hotscot
/**
 * call the WIB API and convert urls into trackable urls
 *
 * @param mixed $urls 
 * @return void
 * @author Craig Ulliott
 */
function track_urls($urls, $additional_params = null)
{
    // to make the code easier, and accept one or an array of urls
    if (!is_array($urls)) {
        $urls = array($urls);
        $return_single = true;
    } else {
        $return_single = false;
    }
    // save on bandwidth
    $unique_urls = array_unique($urls);
    // here we add any new params we want to the url
    if ($additional_params) {
        foreach ($unique_urls as $k => $url) {
            // we cant add params to some urls
            if (!stristr($url, 'ad.doubleclick.net')) {
                $unique_urls[$k] = http_add_params($url, $additional_params);
            }
        }
    }
    // build an assoc array to return the new urls
    $new_urls = array();
    // build cache keys
    $cache_keys = array();
    foreach ($unique_urls as $url) {
        $cache_keys[] = 'track_urls_' . $url;
    }
    // try the cache first
    foreach (Cache::get($cache_keys) as $cache_key => $lilurl) {
        $url = substr($cache_key, 11);
        // add to the new url list
        $new_urls[$url] = $lilurl;
        // remove from the $unique_urls queue
        $k = array_search($url, $unique_urls);
        if ($k !== false) {
            unset($unique_urls[$k]);
        }
    }
    // we want to get all the remaining lilurls in one call
    WIB::getClient()->start_batch();
    // loop through each
    foreach ($unique_urls as $url) {
        // if we didnt get it from the cache
        if (!array_key_exists($url, $new_urls)) {
            $response = WIB::getClient()->call_method('lilurl.create', array('url' => $url));
        }
    }
    // get the results
    $results = WIB::getClient()->run_batch();
    // get only the parts we want from each URL
    foreach ($results as $result) {
        //the url we passed to the wib api
        $tracked_url = array_val($result, array('results', 0, 'url'));
        // we now go back to the very original $urls array, as we dont want to pass back the "additional params"
        $k = array_search($tracked_url, $unique_urls);
        $original_url = $urls[$k];
        $tracking_url = array_val($result, array('results', 0, 'lilurl'));
        if ($original_url && $tracking_url) {
            // add it to the result
            $new_urls[$original_url] = $tracking_url;
            // cache it for next time
            $from_cache = Cache::add('track_urls_' . $original_url, $tracking_url);
        }
    }
    if ($return_single) {
        return reset($new_urls);
    }
    return $new_urls;
}