我在我的codeigniter项目中使用了curl来运行API。在本地环境中一切正常。但是在live服务器上出现了问题。
我使用 AJAX 获取数据,然后使用curl调用api。
在我的视图中:
$.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");
}
});
在控制器中
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');
}
和控制器中的My Api_call函数
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;
}
此代码在localhost上运行,但在实时服务器上,我得到500 internal server
错误和以下错误消息
Message: Call to a member function children() on bool
控制器vacancyservice()
中的$data['response'] = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->GetVacancyFeedResponse;
行上指示错误功能
调试结果
经过相当长时间的调试后,我发现在我的api_call()
函数中,$response
由于某种原因为空,它应该包含XML响应(在localhost上工作正常)
1条答案
按热度按时间v7pvogib1#
我调试了相当长的时间,找到了一个解决方案
我补充道
而且成功了!