0

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)

Leena Patel
  • 2,423
  • 1
  • 14
  • 28
  • 1
    is your PHP version on local, is same as the PHP version on live? – M.Haris Nov 11 '22 at 12:05
  • 1
    Have you debugged the `curl` call? Eg what do `curl_getinfo()` and `curl_error()` show? Have you tried `CURLOPT_VERBOSE` etc to see more of the request? https://stackoverflow.com/questions/3757071/php-debugging-curl, https://stackoverflow.com/questions/36591484/php-curl-working-in-localhost-but-not-working-on-server – Don't Panic Nov 11 '22 at 23:41
  • 1
    [Is php-curl installed on your server](https://stackoverflow.com/questions/62897481/php-curl-working-on-one-localhost-but-not-on-other)? Is there a reason you are using `CURLOPT_ENCODING=''`? And `CURL_HTTP_VERSION_1_1`? It isn't a problem but `CURLOPT_CUSTOMREQUEST` is not necessary when you are POSTing. – Don't Panic Nov 11 '22 at 23:41
  • Thank you for your time i have solved this will be posting the solution – Leena Patel Nov 12 '22 at 06:06
  • Topical: [cURL requires CURLOPT_SSL_VERIFYPEER=FALSE](https://stackoverflow.com/q/18971983/2943403) and [If CURLOPT_SSL_VERIFYPEER is false, is the data transfer no longer secure?](https://stackoverflow.com/q/4660610/2943403) and [PHP CURL CURLOPT_SSL_VERIFYPEER ignored](https://stackoverflow.com/q/15135834/2943403) – mickmackusa Nov 12 '22 at 07:12

1 Answers1

0

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

I added

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

And It worked!

Leena Patel
  • 2,423
  • 1
  • 14
  • 28