如何在 codeigniter 中访问 rest api 中的 curl post 数据?
How to access curl post data in a rest api in codeigniter?
我想使用 curl post 来自 Codeigniter 控制器的数据 API。
我无法访问 API 中的 post 数据。
这是我的 CURL 代码。
...
class SearchJobs extends CI_Controller {
public function index()
{
$headers = array(
'Content-Type:application/json'
);
$fields=$this->input->post();
/////////////////////get jobs/////////////////
$api_path=API_PATH."index.php/searchJobs/getJobs";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_path);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));
$featuredJobs = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
exit();
} else {
// check the HTTP status code of the request
$resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($resultStatus != 200) {
echo stripslashes($featuredJobs);
die('Request failed: HTTP status code: ' . $resultStatus);
}
$featured_jobs_array=(array)json_decode($featuredJobs);
print_r($featured_jobs_array);
exit();
}
$this->load->view('searchjobs/index',array('featuredJobs'=>$featured_jobs_array));
}
}
...
这就是我在 API:
中访问它的方式
...
public function getJobs_post()
{
$data_array=array();
$res_array=array();
$pageNo = $this->input->get('pageNo');
$where = ' j.isActive="y" and isApproved=1 ';
$posted_skills=$this->input->get('skills');
$locations=$this->input->get('location');
...
但是,我可以使用相同的方法从 postman 轻松访问 API 中的 post 数据 $this->input->get('skills');
。
删除JsonHeader
$headers = 数组(
'Content-Type:application/json'
);
和
post数据直接不用json_encode
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
我想使用 curl post 来自 Codeigniter 控制器的数据 API。 我无法访问 API 中的 post 数据。 这是我的 CURL 代码。 ...
class SearchJobs extends CI_Controller {
public function index()
{
$headers = array(
'Content-Type:application/json'
);
$fields=$this->input->post();
/////////////////////get jobs/////////////////
$api_path=API_PATH."index.php/searchJobs/getJobs";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_path);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));
$featuredJobs = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
exit();
} else {
// check the HTTP status code of the request
$resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($resultStatus != 200) {
echo stripslashes($featuredJobs);
die('Request failed: HTTP status code: ' . $resultStatus);
}
$featured_jobs_array=(array)json_decode($featuredJobs);
print_r($featured_jobs_array);
exit();
}
$this->load->view('searchjobs/index',array('featuredJobs'=>$featured_jobs_array));
}
}
...
这就是我在 API:
中访问它的方式...
public function getJobs_post()
{
$data_array=array();
$res_array=array();
$pageNo = $this->input->get('pageNo');
$where = ' j.isActive="y" and isApproved=1 ';
$posted_skills=$this->input->get('skills');
$locations=$this->input->get('location');
...
但是,我可以使用相同的方法从 postman 轻松访问 API 中的 post 数据 $this->input->get('skills');
。
删除JsonHeader $headers = 数组( 'Content-Type:application/json' );
和 post数据直接不用json_encode curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);