0

API にフックするためのフィード ジェネレーターを構築しようとしています。フックしようとしている API は、次の形式で URL を構築します。

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

ご覧のとおり、内側のフィード オプションは括弧で囲まれています。内側のオプションの 1 つを引き出した場合、括弧で囲む必要がある他のオプションが残りますか?

この最終結果を優雅に達成するPHPジェダイマインドトリックの方法はありますか? または、途方もなく長い if/else/ifelse ループを構築する必要がありますか?

ありがとう!

4

1 に答える 1

0

さて、私はこれを解決するのに少し時間を費やしました。私が思いつくことができる最高のものは以下です。基本的に、この関数は、必要なパラメーターのキーと値のペアで満たされた配列を受け入れます。最初にパラメーターをループして合計数を見つけ、次にフロントエンドに必要な括弧を適用します。次に、一連のループを介してパラメーターを解析し、必要に応じて追加します。

このソリューションは Mochi Feed API に固有のものですが、少し調整すれば他の方法でも使用できます。

// 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;
}
于 2012-08-15T17:04:34.287 に答える