1

I want to post data from the Codeigniter controller using curl to API. I'm not able to access the post data in API. Here's my CURL code. ...

     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));
}

}

...

and this is how I'm accessing it in the 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');

...

But, I can easily access the post data in API from postman using the same method $this->input->get('skills');.

2
  • You are sending a POST request, but you are using $this->input->get …? Commented May 13, 2020 at 8:29
  • @CBroe Yes i was using get because i was testing data through postman with query params. Then i changed that to form-data in body. Then it worked with $this->input->post Commented May 13, 2020 at 8:39

1 Answer 1

3

Remove Json Header $headers = array( 'Content-Type:application/json' );

and post data directly without json_encode curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.