【问题标题】:how to pass data in get request for php api hit?如何在获取 php api 命中请求中传递数据?
【发布时间】:2020-05-26 12:09:08
【问题描述】:

我通过“GET”请求在邮递员的正文部分中传递数据,并使用此 URL: “http://localhost:8888/wordpress/wp-json/gh/v3/contacts”。 我传递的正文中的原始数据是:

 {
 "query": {
    "tags_include" : "92"
 }
}

这将返回包含标签 ID“92”的所有学生,而没有此原始正文数据链接“http://localhost:8888/wordpress/wp-json/gh/v3/contacts”将返回数据库中存在的所有学生。 现在我的查询是我试图在我的代码中传递这个正文部分:

$tag_args = array(
        "query" => array(
        "tags_include"=>$tag_id
        )
    );
$all_tag_students = $gr->get_tag_contact($tag_args);

get_tag_contact 是用正文点击 URL“http://localhost:8888/wordpress/wp-json/gh/v3/contacts”的函数

 {
 "query": {
 "tags_include" : "92"
 }
}

这是我为此创建的函数:

  function get_tag_contact($tag_args){
    $emails = array();
    $args = $this->args;
    $args['method'] = 'GET';
    $args['body'] = json_encode($tag_args);
    $response = wp_remote_get($this->apiurl.'contacts',$args);
    $body = json_decode( wp_remote_retrieve_body( $response ) );
    if(!empty($body)){
        foreach ($body->contacts as $contact) 
        {
            $emails[] = $contact->data->email;
        }
        return $emails;
    }
}

但这里的问题是它返回所有学生,而不仅仅是包含 ID 的特定学生

 Array
(
   [query] => Array
    (
        [tags_include] => 92
    )

)

我怎样才能找到包含 id "92" 的学生只有我在这里缺少的东西?

【问题讨论】:

  • GET 请求不支持在正文中传递数据。如果要在 GET 请求中传递数据,请使用查询字符串。

标签: php json postman


【解决方案1】:

我在这里发现了错误:

function get_tag_contact($tag_args){
    $emails = array();
    $args = $this->args;
    $args['method'] = 'GET';
    $args['body'] = $tag_args;
    $response = wp_remote_get($this->apiurl.'contacts',$args);
    $body = json_decode( wp_remote_retrieve_body( $response ) );
    if(!empty($body)){
        foreach ($body->contacts as $contact) 
        {
            $emails[] = $contact->data->email;
        }
        return $emails;
    }
}

我正在编码 $tag_args。

$args['body'] = $tag_args;

我只需要从这一行中删除 json_encode 就可以了。

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 2018-11-06
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多