我想使用 curl 將來自 Codeigniter 控制器的數(shù)據(jù)發(fā)布到 API。我無法訪問 API 中的帖子數(shù)據(jù)。這是我的 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');...但是,我可以使用相同的方法從郵遞員輕松訪問 API 中的發(fā)布數(shù)據(jù)$this->input->get('skills');。
1 回答

泛舟湖上清波郎朗
TA貢獻1818條經(jīng)驗 獲得超3個贊
刪除 Json 標頭 $headers = array( 'Content-Type:application/json' );
并 在沒有 json_encode 的情況下直接發(fā)布數(shù)據(jù) curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
- 1 回答
- 0 關(guān)注
- 117 瀏覽
添加回答
舉報
0/150
提交
取消