Domanda

I'm trying to build a feed generator for hooking into an api. The API that I'm trying to hook into builds URLs in the following format:

http://the.apiurl.com/f/query/?q=((((((rating%3A%3E%3D3)%20and%20rev_enabled)%20and%20not%20tags%3Arandomtag)%20and%20languages%3Aen)%20and%20this_enabled)%20and%20that_enabled)%20and%20this_also_enabled&_id=xxxx&limit=100

As you can see, the inner feed options are wrapped in parentheses. If you were to pull out one of the inner options, you would still be left would other options that must be wrapped in parentheses?

Is there some PHP jedi mind trick way of achieving this end result with elegance? OR do I have to simply build a ridiculously long if/else/ifelse loop?

Thanks!

È stato utile?

Soluzione

Well I spent some time working this one out. The best I could come up with is below. Basically, the function accepts an array filled with key/value pairs for the necessary parameters. It first loops through the parameter/s to find the total count and then applies the necessary parentheses on the front end. Then it parses the parameters via a series of loops and appends them as necessary.

The solution is specific to the Mochi Feed API, but it can be used in other ways if you were to tweak it a bit.

// Accepts: $params (array) - An array which contains all of the filter options we are trying to account for.
function process_feed($params=null) {

    // Grab our feed's base URL
    $url = 'http://the.apiurl.com/f/query/?q=';

    // The items stored in this array can be set to a boolean value (true or false)
    $bool_queries = array('rev_enabled', 'this_enabled', 'this_also_enabled');

    // The items stored in this array can be set to an integer, string, etc...
    $return_queries = array('languages', 'omit_tags', 'tags', 'category', 'limit');

    // Grab the parameter/filter count
    $size = count($params);

    // We have enough filters to go custom
    if ($size > 1) {

        // Loop through and place the appropriate amount of opening parens
        for ($x = 0; $x < ($size-1); $x++) { $url .= '('; }

        $i = 0;

        foreach ($params as $key=>$value) {

            if ($i < ($size-1)) {

                if (in_array($key, $bool_queries)) {
                    $url .= $key.')%20and%20';
                } else {

                    // special NOT case
                    if ($key == 'omit_tags') {
                        $url .= 'not%20tags%3A'.$value.')%20and%20';
                    } else {
                        $url .= $key.'%3A'.$value.')%20and%20';
                    }
                }

            } else {
                if (in_array($key, $bool_queries)) {
                    $url .= $key;
                } else {
                    // special NOT case
                    if ($key == 'omit_tags') {
                        $url .= 'not%20tags%3A'.$value;
                    } else {
                        $url .= $key.'%3A'.$value;
                    }
                }
            }

            $i++;
        }

    } else if ($size == 1) {

        foreach ($params as $key=>$value) {
            if (in_array($key, $bool_queries)) {
                $url .= $key;
            } else {
                // special NOT case
                if ($key == 'omit_tags') {
                    $url .= 'not%20tags%3A'.$value;
                } else { 
                    $url .= $key.'%3A'.$value;
                }
            }
        }
    }

    if (isset($params['limit'])) {
        $url .= '&partner_id=' . $our_partner_key . '&limit=' . $limit;
    } else {
        $url .= '&partner_id=' . $our_partner_key;
    }

    if(isset($url)) {
        return $url;
    }

    return false;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top