2017-04-26 96 views
-1

我正在嘗試使用SoftLayer::API::SOAP獲取global_ip_id。但是,當我嘗試從Softlayer API獲取全球IP

$global_ip_id = $client->getGlobalIpRecords()->result->[0]->{id}; 

我得到的錯誤:

Can't use an undefined value as an ARRAY reference at /usr/bin/reroute_global line 19

+0

歡迎來到[so]!在這個網站,你應該嘗試**自己編寫代碼**。後** [做更多的研究](//meta.stackoverflow.com/questions/261592)**如果你有問題,你可以**發佈你已經嘗試**與清楚的解釋是什麼是'工作**並提供[** Minimal,Complete和Verifiable示例**](// stackoverflow.com/help/mcve)。我建議閱讀[問]一個好問題和[完美問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要參加[遊覽]並閱讀[this](// meta.stackoverflow.com/questions/347937/)**。 – Badacadabra

回答

0

我不知道什麼框架,或者你正在使用的語言,但我prettry肯定,這是錯誤的使用是一個問題。

SOAP響應,你從電話獲得應該是somethign這樣

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.service.softlayer.com/soap/v3.1/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Header> 
    <ns1:totalItems> 
     <amount>7</amount> 
    </ns1:totalItems> 
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
    <ns1:getGlobalIpRecordsResponse> 
     <getGlobalIpRecordsReturn SOAP-ENC:arrayType="ns1:SoftLayer_Network_Subnet_IpAddress_Global[7]" xsi:type="ns1:SoftLayer_Network_Subnet_IpAddress_GlobalArray"> 
     <item xsi:type="ns1:SoftLayer_Network_Subnet_IpAddress_Global"> 
      <description xsi:nil="true"/> 
      <destinationIpAddressId xsi:nil="true"/> 
      <id xsi:type="xsd:int">11111</id> 
      <ipAddressId xsi:type="xsd:int">22222</ipAddressId> 
      <typeId xsi:type="xsd:int">1</typeId> 
     </item> 
     </getGlobalIpRecordsReturn> 
    </ns1:getGlobalIpRecordsResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

你現在如何獲取數據從響應取決於語言或框架所使用,作爲響應是一些語言中的XML需要瀏覽XML標籤,例如

result->{Body}->{getGlobalIpRecordsResponse}->{getGlobalIpRecordsReturn }->[0]->{id} 

所以我建議你要確保你的語言或框架如何使您可以通過SOAP響應導航,因爲目前你所得到的問題是由於您試圖訪問的方式不對數據。

現在,如果你使用的是SoftLayer的Perl客戶,你應該使用這樣的事情:

my $client = SoftLayer::API::SOAP->new('SoftLayer_Account', undef, $api_username, $api_key); 
my $output = $client->getGlobalIpRecords(); 
print $output->result->[0]->{'id'}; 

正如你所看到的一切都取決於你所使用

而且萬一你是什麼lenguage和框架使用Perl的錯誤可能是由於結果爲空,在這種情況下,您需要驗證這不是空的,請參閱Error: Can't use an undefined value as an ARRAY reference以獲取更多信息。

Regards

+0

嗨尼爾森,謝謝你的答覆。我使用Softlayer的Perl,我試過你的例子,但我仍然有相同的錯誤: –

+0

我跟着這個實現https://serverfault.com/questions/666850/softlayer-haproxy-with-failover –

+0

也許你沒有任何全局IP嘗試轉儲結果並查看它是否有數據「print Dumper($ output-> result);」 –