codeigniter-使用simple_html_dom.php从api获取数据
我对如何从API获取数据感到困扰 我有一个API,它将此作为响应返回给我 (请注意,这只是代码的一部分,以使其简短易懂。) 在codeigniter-使用simple_html_dom.php从api获取数据,php,codeigniter,simple-html-dom,Php,Codeigniter,Simple Html Dom,我对如何从API获取数据感到困扰 我有一个API,它将此作为响应返回给我 (请注意,这只是代码的一部分,以使其简短易懂。) 在execApi函数中,我有这个代码 protected function execApi($option, $returnName="", $sleep=1) { $curl = curl_init(); sleep($sleep); curl_setopt_array($curl, $option); $response = curl
execApi
函数中,我有这个代码
protected function execApi($option, $returnName="", $sleep=1) {
$curl = curl_init();
sleep($sleep);
curl_setopt_array($curl, $option);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$this->errMsg = "cURL Error #:" . $err;
return false;
} else {
$response = mb_convert_encoding($response, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$obj = json_decode($json, true);
if ($obj === NULL) {
$this->errMsg = "Parse error: syntax error.";
return false;
}
if (empty($obj["error_code"])){
if (empty($returnName)){
$this->returnArray = $obj;
return true;
} else {
if (empty($obj[$returnName])){
$this->errMsg = print_r($obj, true);
return false;
} else {
$this->returnArray = $obj[$returnName];
return true;
}
}
} else {
$this->errMsg = $obj["error_message"];
return false;
}
}
}
所以在我的模型中,到目前为止我已经有了这个代码
(也是模型的一部分,用于获取API的特定函数。)
但是我不能得到任何结果。我很沮丧,因为这花了我很长时间。我看到了一些问题。您的cdata已损坏,而且
$items=$item
我假设应该是$items[]=$item
,您应该在循环的顶部初始化$item
,在循环的外部初始化$items
,这只是乍一看,我发现了一些问题。您的cdata已损坏,而且$items=$item
我假设应该是$items[]=$item
,您应该在循环的顶部初始化$item
,在循环的外部初始化$items
,这只是乍一看
public function getCompany($industryId,$companyId,$entryName,$priority,$searchNum)
{
$option = array( //API link here);
$flg = $this->execApi(
$option
);
return $flg;
}
protected function execApi($option, $returnName="", $sleep=1) {
$curl = curl_init();
sleep($sleep);
curl_setopt_array($curl, $option);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$this->errMsg = "cURL Error #:" . $err;
return false;
} else {
$response = mb_convert_encoding($response, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$obj = json_decode($json, true);
if ($obj === NULL) {
$this->errMsg = "Parse error: syntax error.";
return false;
}
if (empty($obj["error_code"])){
if (empty($returnName)){
$this->returnArray = $obj;
return true;
} else {
if (empty($obj[$returnName])){
$this->errMsg = print_r($obj, true);
return false;
} else {
$this->returnArray = $obj[$returnName];
return true;
}
}
} else {
$this->errMsg = $obj["error_message"];
return false;
}
}
}
<?php
require_once(APPPATH."libraries/simple_html_dom.php");
class CompanyModel extends CI_Model
{
public function getDetail($industryId,$companyId,$entryName,$priority,$searchNum)
{
$result = $this->itpApi->getCompany($industryId,$companyId,$entryName,$priority,$searchNum);
$itpCompanyDetail = $this->itpApi->getReturnArray();
$xml = simplexml_load_string($itpCompanyDetail);
$html = new simple_html_dom();
$html->load($xml->retdata);
foreach($html->find('body') as $home) {
//populate all items
$item['name'] = $home->find('div[id="home"] div[id="topinfo"] h1[id="basetop"]', 0)->plaintext;
$item['email'] = $home->find('div[id="home"] div[id="mainblock"] div[class="txtblock"]', 8)->plaintext;
$item['tel'] = $home->find('div[id="home"] div[id="topinfo"] p', 2)->plaintext;
$item['fax'] = $home->find('div[id="home"] div[id="mainblock"] div[class="txtblock"] p', 3)->plaintext;
$items = $item;
}
log_message('debug', 'items = ' . print_r($items, true));
//$this->getItems($items);
}
}
?>
Array
(
[name] => 鰻駒形前川
[email] => m-k105@agate.plala.or.jp
[tel] => 03-3841-6314
[fax] => 03-3844-8851
)