Home > Software engineering >  Codeigniter : Curl API Call not giving response on live server
Codeigniter : Curl API Call not giving response on live server

Time:11-13

I have used curl to run API in my codeigniter project. Everything works fine on local environment. But the problem arises on live server.

I have used ajax to get data which then uses curl to call api .

In My View :

$.ajax({
    type: "POST",
    url: "<?php echo site_url($websitepagename.'/vacancyservice/')?>",
    data: "job_category=" job_category "&location=" location "&contract_type=" contract_type "&search_keyword=" search_keyword,
    success: function(data)
    {
      $("#ajaxBind").html(data);
      // $("html, body").animate({ scrollTop: 0 }, "slow");
    }
  });

In my Controller

function vacancyservice() {     
    $data['response'] = $this->api_call();
    $soap     = simplexml_load_string($data['response']);
    $data['response'] = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->GetVacancyFeedResponse;
    $this->load->vars($data);
    $this->load->view('ajax_vacancy_list');
}

And My Api_call function in controller

function api_call() {
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => ' API URL ',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
        <GetVacancyFeed xmlns="http://www1.sniperhire.net/sddf">
        <FeedID>FEEDID</FeedID>
        <Username>USERNAME</Username>
        <Password>PASSWORD</Password>
        </GetVacancyFeed>
        </soap:Body>
        </soap:Envelope>',
        CURLOPT_HTTPHEADER => array(
            'Content-Type: text/xml; charset=utf-8',
            'SOAPAction: SOAP URL'
        ),
    ));

    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

This Code runs on localhost But on live server i am getting 500 internal server error and below error message

Message: Call to a member function children() on bool

Error indicates on line $data['response'] = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->GetVacancyFeedResponse; in controller vacancyservice() Function

Debugging Result

After Debugging for Quite a lot time I found that in my api_call() function $response is null for some reason which should contain XML response (Which works fine on localhost)

CodePudding user response:

I debugged for quite a time and found a solution to this

I added

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

And It worked!

  • Related