如何在获取 php api 命中请求中传递数据?

我正在通过“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”的学生,只有我在这里缺少什么?


泛舟湖上清波郎朗
浏览 104回答 1
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,它就可以完美地工作。
打开App,查看更多内容
随时随地看视频慕课网APP