简体   繁体   English

为什么我从请求中收到的响应没有正确过滤?

[英]Why is the response I am receiving from my request not filtering correctly?

Good day,再会,

I am trying to integrate an API into a Wordpress site.我正在尝试将API集成到Wordpress站点中。

API Documentation API 文档

I have tested the AP I using Postman and I am able to get the correct response.我已经使用Postman测试了AP ,我能够得到正确的响应。

Reading the documentation and various other sources I have come up with the following code to list the various results and then perform a search on the results.阅读文档和其他各种来源,我想出了以下代码来列出各种结果,然后对结果执行搜索。

public function search_jobs( $param = [], $filter = [] ){
    $update_token                   = [];

    $this->id_token                 = $this->settings_obj->get_setting( 'id_token' );
    
    $response_arr                   = [];
    $response_arr['code']           = 0;
    $response_arr['alert']          = "";
    $response_arr['data']           = [];

    $endpoint                       = $this->base_url . "job/search/fl=id,job_title,location,created_date,industry,public_description,summary;sort=published_date desc";

    if( $filter && !empty( $filter ) ){

        $search_string = isset( $filter['vj_search'] ) ? sanitize_text_field( $filter['vj_search'] ) : "";

        $search_industry = isset( $filter['industry'] ) ? $filter['industry'] : [];
        $search_location = isset( $filter['location'] ) ? $filter['location'] : [];
        $search_function = isset( $filter['function'] ) ? $filter['function'] : [];

        $search_filter_qry = [];
            $search_filter_qry[] = "fq=private_job:0";

        if( $search_industry && !empty( $search_industry ) ){
            $search_filter_qry[] = "fq=sub_industry_id:" . implode( " OR ", $search_industry );
        }

        if( $search_location && !empty( $search_location ) ){
            $search_filter_qry[] = "fq=country_code:" . implode( " OR ", $search_location );
        }

        if( $search_function && !empty( $search_function ) ){
            $search_filter_qry[] = "fq=sfe_id:" . implode( " OR ", $search_function );
        }

        $search_filter_qry_str = implode( "%23&", $search_filter_qry );

        if( !empty( $search_filter_qry_str ) ){
            $endpoint = $endpoint . "?" . $search_filter_qry_str . "%23";
        }

        if( !empty( $search_string ) ){
            $endpoint = $endpoint . "&q=text:" . $search_string . "%23";
        }

I am able to get the response on the page required and I am also able to search by location and industry, however I have the following issue :我能够在所需页面上获得响应,我也能够按位置和行业进行搜索,但是我有以下问题

The result includes all results where I am trying to only get jobs that are not private $search_filter_qry[] = "fq=private_job:0";结果包括我试图只获取非私有工作的所有结果$search_filter_qry[] = "fq=private_job:0";

If I edit $endpoint and change it to:如果我编辑$endpoint并将其更改为:

$endpoint                       = $this->base_url . "job/search/fl=id,job_title,location,created_date,industry,public_description,summary;sort=published_date desc?fq=private_job:0 %23";

I get the correct response on the original display of jobs with only results that are public, but then I am unable to perform any searches.我在工作的原始显示上得到了正确的响应,只有公开的结果,但随后我无法执行任何搜索。

I understand it is because of the concatenation that Is then creating the wrong request when I edit the $endpoint and it returns 0 results found.我明白这是因为当我编辑$endpoint并返回 0 结果时创建错误的请求的连接。

I have spent a couple of days on this by changing for example $search_filter_qry[] = "fq=private_job:0";我花了几天时间通过更改例如$search_filter_qry[] = "fq=private_job:0"; to $search_filter_qry[] = "?fq=private_job:0";$search_filter_qry[] = "?fq=private_job:0"; by adding in the ?.通过添加?。 I will be honest I am not to comfortable yet with the SOLR Backend that the API uses.老实说,我对API使用的SOLR Backend还不满意。

Can anyone please have a fresh look at my code and give me an idea as to what I am missing please.任何人都可以重新审视我的代码,并让我知道我缺少什么。

I was able to find the solution as follows:我能够找到解决方案如下:

I changed the $endpoint before the if statement to:我将if语句之前的$endpoint更改为:

$endpoint                       = $this->base_url . "job/search/fl=id,job_title,location,created_date,industry,public_description,summary;sort=published_date desc?fq=private_job:0%23";

And then after the initial if statement ,然后在最初的if statement

if( $filter && !empty( $filter ) ){

I added the $endpoint as我将$endpoint添加为

$endpoint                       = $this->base_url . "job/search/fl=id,job_title,location,created_date,industry,public_description,summary;sort=published_date desc";

This allowed the concatenation to work correctly if the search parameters are not empty.如果搜索参数不为空,这允许连接正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM