所以我有这个函数来获取API响应。
以下是API返回响应的内容;
<?xml version="1.0" encoding="UTF-8"?>
<Response type="1">
<ack>
<ackstatus>OK</ackstatus>
<ackreason></ackreason>
</ack>
<dataversion>1</dataversion>
<hitcount>13391</hitcount>
<shops>
<spad>
</spad>
<basead>
<shop>
<id>KN0100060500216636</id>
<priority>1</priority>
<entryname>旭川職業能力開発促進センター</entryname>
<telno>0166-48-2412</telno>
<zipcode>079-8418</zipcode>
<address>北海道旭川市永山8条20丁目3−1</address>
<latitude>157707796</latitude>
<longitude>512803967</longitude>
<imageurl></imageurl>
<promoword></promoword>
<coupon>0</coupon>
<group>0</group>
<searchnum>7214223</searchnum>
</shop>
<shop>
<id>KN0100060500202256</id>
<priority>1</priority>
<entryname>旭川市立/嵐山小中学校</entryname>
<telno>0166-61-1199</telno>
<zipcode>070-8051</zipcode>
<address>北海道旭川市江丹別町嵐山143</address>
<latitude>157704475</latitude>
<longitude>512195888</longitude>
<imageurl></imageurl>
<promoword></promoword>
<coupon>0</coupon>
<group>0</group>
<searchnum>7214223</searchnum>
</shop>
</basead>
</shops>
</Response>
所以在我的函数中我必须获取一些细节,例如; id,entryname,以便我可以将它们用作参数来获取不同的API:
这是功能; (我把日志放进去,如果我运行脚本就会显示结果。)
public function getcompList($prefectureId,$industryId,$offset)
{
$itpCompanies = $this->itpApi->getCompanies($prefectureId,$industryId,$offset);
$hascompany = $this->itpApi->getReturnArray();
log_message('debug','Company list fetched, returning...');
return $hascompany;
}
public function getcompanyList($hascompany,$prefectureId,$offset,$industryId)
{
foreach ( $hascompany['shops']['basead']['shop'] as $company ) {
$companyId = $company['id'];
$entryName = $company['entryname'];
$priority = $company['priority'];
$searchNum = $company['searchnum'];
log_message('debug', 'Comp_List API: company_id = ' . print_r($companyId, true));
log_message('debug', 'Comp_List API: company_name = ' . print_r($entryName, true));
log_message('debug', 'Comp_List API: company_prio = ' . print_r($priority, true));
log_message('debug', 'Comp_List API: company_searchno = ' . print_r($searchNum, true));
log_message('debug', 'Company_List->Company_Detail:: Getting Company Detail...');
$this->compModel->getDetail($prefectureId,$offset,$industryId,$companyId,$entryName,$priority,$searchNum);
}
}
例如,在上面的API响应中,日志将显示;
Company list fetched, returning...
Comp_List API: company_id = KN0100060500216636
Comp_List API: company_name = 旭川職業能力開発促進センター
Comp_List API: company_prio = 1
Comp_List API: company_searchno = 7214223
Company_List->Company_Detail:: Getting Company Detail...
Company Detail Inserted!
Comp_List API: company_id = KN0100060500202256
Comp_List API: company_name = 旭川市立/嵐山小中学校
Comp_List API: company_prio = 1
Comp_List API: company_searchno = 7214223
Company_List->Company_Detail:: Getting Company Detail...
Company Detail Inserted!
问题是当API响应仅返回1 <shop>
时。它给了我这个;
Comp_List API: company_id = K
Comp_List API: company_name = K
Comp_List API: company_prio = K
Comp_List API: company_searchno = K
Company_List->Company_Detail:: Getting Company Detail...
因为这个函数也在一个检查并获取API的循环中,结果会给我错误;重复查询等。
可能是API中的问题吗?或foreach
函数中的getcompanyList
循环?
答案 0 :(得分:0)
我不确切地知道你为什么会遇到单个结果的问题,但是因为你提供了XML我使用它并制作了2个版本,一个有两个结果,一个有一个结果。我改变了你的循环,无论我使用哪个版本的XML,我都能获得数据。这是一个完整的例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php
function logCompanyData( $shops )
{
foreach ( $shops as $company )
{
$companyId = $company->id;
$entryName = $company->entryname;
$priority = $company->priority;
$searchNum = $company->searchnum;
echo 'debug: Comp_List API: company_id = ' . $companyId . '<br />';
echo 'debug: Comp_List API: company_name = ' . $entryName . '<br />';
echo 'debug: Comp_List API: company_prio = ' . $priority . '<br />';
echo 'debug: Comp_List API: company_searchno = ' . $searchNum . '<br />';
echo 'debug: Company_List->Company_Detail:: Getting Company Detail...' . '<br /><br />';
}
}
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Response type="1">
<ack>
<ackstatus>OK</ackstatus>
<ackreason></ackreason>
</ack>
<dataversion>1</dataversion>
<hitcount>13391</hitcount>
<shops>
<spad></spad>
<basead>
<shop>
<id>KN0100060500216636</id>
<priority>1</priority>
<entryname>旭川職業能力開発促進センター</entryname>
<telno>0166-48-2412</telno>
<zipcode>079-8418</zipcode>
<address>北海道旭川市永山8条20丁目3−1</address>
<latitude>157707796</latitude>
<longitude>512803967</longitude>
<imageurl></imageurl>
<promoword></promoword>
<coupon>0</coupon>
<group>0</group>
<searchnum>7214223</searchnum>
</shop>
<shop>
<id>KN0100060500202256</id>
<priority>1</priority>
<entryname>旭川市立/嵐山小中学校</entryname>
<telno>0166-61-1199</telno>
<zipcode>070-8051</zipcode>
<address>北海道旭川市江丹別町嵐山143</address>
<latitude>157704475</latitude>
<longitude>512195888</longitude>
<imageurl></imageurl>
<promoword></promoword>
<coupon>0</coupon>
<group>0</group>
<searchnum>7214223</searchnum>
</shop>
</basead>
</shops>
</Response>';
$xml2 = '<?xml version="1.0" encoding="UTF-8"?>
<Response type="1">
<ack>
<ackstatus>OK</ackstatus>
<ackreason></ackreason>
</ack>
<dataversion>1</dataversion>
<hitcount>13391</hitcount>
<shops>
<spad></spad>
<basead>
<shop>
<id>KN0100060777777777</id>
<priority>7</priority>
<entryname>旭川職業能力開発促進センター</entryname>
<telno>0166-48-7777</telno>
<zipcode>079-7777</zipcode>
<address>北海道旭川市永山8条20丁目3−1</address>
<latitude>157707796</latitude>
<longitude>512803967</longitude>
<imageurl></imageurl>
<promoword></promoword>
<coupon>0</coupon>
<group>0</group>
<searchnum>7777777</searchnum>
</shop>
</basead>
</shops>
</Response>';
$xml = simplexml_load_string( $xml );
logCompanyData( $xml->shops->basead->shop );
$xml2 = simplexml_load_string( $xml2 );
logCompanyData( $xml2->shops->basead->shop );
?>
</body>
</html>