你好,
我正在尝试将API
集成到Wordpress
站点中。
API文件
我已经用Postman
测试了AP
I,并且我能够得到正确的响应。
通过阅读文档和各种其他来源,我想出了以下代码来列出各种结果,然后对结果进行搜索。
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";
}
我能够在所需页面上获得响应,也能够按位置和行业进行搜索,但我有以下问题:
结果包括我试图只获得非私有$search_filter_qry[] = "fq=private_job:0";
的作业的所有结果
如果我编辑$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";
我在最初显示的作业中得到了正确的响应,只有公开的结果,但随后我无法执行任何搜索。
我知道这是因为当我编辑$endpoint
时,is创建了错误的请求,并且它返回了0个找到的结果。
我在这方面花了几天时间,例如通过添加?将$search_filter_qry[] = "fq=private_job:0";
更改为$search_filter_qry[] = "?fq=private_job:0";
?。老实说,我对API
使用的SOLR Backend
还不满意。
有谁能重新看看我的代码,并告诉我我缺少了什么吗。
我能够找到如下解决方案:
我将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";
然后在初始if statement
之后,
if( $filter && !empty( $filter ) ){
我添加了$endpoint
作为
$endpoint = $this->base_url . "job/search/fl=id,job_title,location,created_date,industry,public_description,summary;sort=published_date desc";
这使得在搜索参数不为空的情况下连接能够正确工作。