所以我有這個函數來獲取API響應。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中的問題?或getcompanyList
函數中的foreach
循環?
只有一個結果時XML結構是否有所不同?此外,使用JSON可以讓生活變得更輕鬆(如果您可以選擇響應類型) –
XML結構相同,返回的數據使用JSON。對不起,我忘了提及我有一個函數來獲取API,然後解析它。 –
因此,如果您可以爲這兩種結果類型提供JSON,我會將其複製並粘貼到示例中,並給出答案。將它作爲代碼添加到您的問題中。 –