2017-09-13 37 views
0

所以我有這個函數來獲取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循環?

+0

只有一個結果時XML結構是否有所不同?此外,使用JSON可以讓生活變得更輕鬆(如果您可以選擇響應類型) –

+0

XML結構相同,返回的數據使用JSON。對不起,我忘了提及我有一個函數來獲取API,然後解析它。 –

+0

因此,如果您可以爲這兩種結果類型提供JSON,我會將其複製並粘貼到示例中,並給出答案。將它作爲代碼添加到您的問題中。 –

回答

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> 
+0

非常感謝你!根據您的代碼示例,我將代碼更改爲; $ companyId = $ company-> id; $ entryName = $ company-> entryname; $ priority = $ company-> priority; $ searchNum = $ company-> searchnum; 也... logCompanyData($ XML-> shops-> basead->店); –