doupingzhi9674 2016-05-12 22:53
浏览 50
已采纳

如果用户没有输入值,如何从HTTP GET请求中排除查询变量?

I am sending a GET request to an external API that works fine if a user enters a value into all applicable fields. I'd like to have all of these fields optional, except of course the first one to ensure we get a response.

Is there a simple method I can use that will ignore these from the request?

These are the variables I'll be passing into the request

$query = $request->input('query');
    $include = $request->input('include');
    $include2 = $request->input('include2');
    $include3 = $request->input('include3');
    $exclude = $request->input('exclude');
    $exclude2 = $request->input('exclude2');

This is the request which may look confusing as a whole but its pretty simple. The user can enter in a base ingredient as a query and further refine their recipe search by entering specific ingredients to include or exclude. I'd like to know how to pass this request if for example the user only entered two included ingredients and one excluded ingredient

$res = $client->get('http://api.yummly.com/v1/api/recipes?_app_id='MY_ID'&_app_key='MY_KEY'&q='.$query.'&allowedIngredient[]='.$include.'&allowedIngredient[]='.$include2.'&allowedIngredient[]='.$include3.'&excludedIngredient[]='.$exclude.'&excludedIngredient[]='.$exclude2.'&maxResult=50', ['auth' =>  ['user', 'pass']]);
        $body = json_decode($res->getBody(), TRUE);

Many thanks for any help on this.

UPDATE-ANSWER

$res = 'http://api.yummly.com/v1/api/recipes?_app_id=MY_ID&_app_key=MY_KEY&q='.$query;

        if($include) {
        $res .= '&allowedIngredient[]='.$include;
        }

        if($include2) {
         $res .= '&allowedIngredient[]='.$include2;
        }

        if($include3) {
         $res .= '&allowedIngredient[]='.$include3;
        }

        if($exclude) {
         $res .= '&excludedIngredient[]='.$exclude;
        }

        if($exclude2) {
         $res .= '&excludedIngredient[]='.$exclude2;
        }

        $res .='&requirePictures=true&maxResult=50';
        $result = $client->get($res , ['auth' =>  ['user', 'pass']]);
        $body = json_decode($result->getBody(), TRUE);
  • 写回答

1条回答 默认 最新

  • dsqe46004 2016-05-12 23:05
    关注

    Easiest method is to just check for a valid value.

    $apiUrl = 'http://api.yummly.com/v1/api/recipes?_app_id='MY_ID'&_app_key='MY_KEY'&q='.$query;
    
    if($include) {
        $apiUrl .= '&allowedIngredient[]='.$include;
    }
    
    if($include2) {
        $apiUrl .= '&allowedIngredient[]='.$include2;
    }
    
    // Etc for all vars
    

    You can also do it by building an array and then imploding it, but this would be quite messy as the keys would need '$allowedIngredient[]' so will let you investigate that route.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算