2016-11-12 71 views
0

如何從HTTP響應中獲取單個值?它實際上是一個JSON響應,但是文本塊就好像它不是JSON。如何從純文本響應中獲取單個數據?

例如,當我查詢WHOIS查詢API,我得到如下回應:

HTTP/1.1 200 
Connection: keep-alive 
Content-Type: application/json;charset=UTF-8 
Date: Sat, 12 Nov 2016 09:10:56 GMT 
Server: Mashape/5.0.6 
Transfer-Encoding: chunked 
X-Ratelimit-Requests-Limit: 11000 
X-Ratelimit-Requests-Remaining: 10991 

{ 
    "available": false, 
    "whoisResponse": "\nWhois Server Version 2.0\n\nDomain names in the .com and .net domains can now be registered\nwith many different competing registrars. Go to http://www.internic.net\nfor detailed information.\n\n Domain Name: TEST.COM\n Registrar: ENOM, INC.\n Sponsoring Registrar IANA ID: 48\n Whois Server: whois.enom.com\n Referral URL: http://www.enom.com\n Name Server: JAY.NS.CLOUDFLARE.COM\n Name Server: RITA.NS.CLOUDFLARE.COM\n Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited\n Updated Date: 02-feb-2016\n Creation Date: 13-sep-2014\n Expiration Date: 13-sep-2017\n\n>>> Last update of whois database: Sat, 12 Nov 2016 09:10:45 GMT <<<\n\nFor more information on Whois status codes, please visit https://icann.org/epp\n\nNOTICE: The expiration date displayed in this record is the date the\nregistrar's sponsorship of the domain name registration in the registry is\ncurrently set to expire. This date does not necessarily reflect the expiration\ndate of the domain name registrant's agreement with the sponsoring\nregistrar. Users may consult the sponsoring registrar's Whois database to\nview the registrar's reported date of expiration for this registration.\n\nTERMS OF USE: You are not authorized to access or query our Whois\ndatabase through the use of electronic processes that are high-volume and\nautomated except as reasonably necessary to register domain names or\nmodify existing registrations; the Data in VeriSign Global Registry\nServices' (\"VeriSign\") Whois database is provided by VeriSign for\ninformation purposes only, and to assist persons in obtaining information\nabout or related to a domain name registration record. VeriSign does not\nguarantee its accuracy. By submitting a Whois query, you agree to abide\nby the following terms of use: You agree that you may use this Data only\nfor lawful purposes and that under no circumstances will you use this Data\nto: (1) allow, enable, or otherwise support the transmission of mass\nunsolicited, commercial advertising or solicitations via e-mail, telephone,\nor facsimile; or (2) enable high volume, automated, electronic processes\nthat apply to VeriSign (or its computer systems). The compilation,\nrepackaging, dissemination or other use of this Data is expressly\nprohibited without the prior written consent of VeriSign. You agree not to\nuse electronic processes that are automated and high-volume to access or\nquery the Whois database except as reasonably necessary to register\ndomain names or modify existing registrations. VeriSign reserves the right\nto restrict your access to the Whois database in its sole discretion to ensure\noperational stability. VeriSign may restrict or terminate your access to the\nWhois database for failure to abide by these terms of use. VeriSign\nreserves the right to modify these terms at any time.\n\nThe Registry database contains ONLY .COM, .NET, .EDU domains and\nRegistrars.\n" 
} 

我想獲得Expiration Date分配給了whoisResponse的變量,這可能嗎?我怎樣才能在PHP中做到這一點?

回答

1

試試這個

$str = "\nWhois Server Version 2.0\n\nDomain names in the .com and .net domains can now be registered\nwith many different competing registrars. Go to http://www.internic.net\nfor detailed information.\n\n Domain Name: TEST.COM\n Registrar: ENOM, INC.\n Sponsoring Registrar IANA ID: 48\n Whois Server: whois.enom.com\n Referral URL: http://www.enom.com\n Name Server: JAY.NS.CLOUDFLARE.COM\n Name Server: RITA.NS.CLOUDFLARE.COM\n Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited\n Updated Date: 02-feb-2016\n Creation Date: 13-sep-2014\n Expiration Date: 13-sep-2017\n\n>>> Last update of whois database: Sat, 12 Nov 2016 09:10:45 GMT <<<\n\nFor more information on Whois status codes, please visit https://icann.org/epp\n\nNOTICE: The expiration date displayed in this record is the date the\nregistrar's sponsorship of the domain name registration in the registry is\ncurrently set to expire. This date does not necessarily reflect the expiration\ndate of the domain name registrant's agreement with the sponsoring\nregistrar. Users may consult the sponsoring registrar's Whois database to\nview the registrar's reported date of expiration for this registration.\n\nTERMS OF USE: You are not authorized to access or query our Whois\ndatabase through the use of electronic processes that are high-volume and\nautomated except as reasonably necessary to register domain names or\nmodify existing registrations; the Data in VeriSign Global Registry\nServices' (\"VeriSign\") Whois database is provided by VeriSign for\ninformation purposes only, and to assist persons in obtaining information\nabout or related to a domain name registration record. VeriSign does not\nguarantee its accuracy. By submitting a Whois query, you agree to abide\nby the following terms of use: You agree that you may use this Data only\nfor lawful purposes and that under no circumstances will you use this Data\nto: (1) allow, enable, or otherwise support the transmission of mass\nunsolicited, commercial advertising or solicitations via e-mail, telephone,\nor facsimile; or (2) enable high volume, automated, electronic processes\nthat apply to VeriSign (or its computer systems). The compilation,\nrepackaging, dissemination or other use of this Data is expressly\nprohibited without the prior written consent of VeriSign. You agree not to\nuse electronic processes that are automated and high-volume to access or\nquery the Whois database except as reasonably necessary to register\ndomain names or modify existing registrations. VeriSign reserves the right\nto restrict your access to the Whois database in its sole discretion to ensure\noperational stability. VeriSign may restrict or terminate your access to the\nWhois database for failure to abide by these terms of use. VeriSign\nreserves the right to modify these terms at any time.\n\nThe Registry database contains ONLY .COM, .NET, .EDU domains and\nRegistrars.\n"; 

$data = preg_match('/Expiration Date: ([0-9]{2})-([a-z]{3})-([0-9]{4})/',$str,$match); 

echo '<pre>'; 
print_r($match); 
echo '</pre>'; 

可以使用preg_match匹配的字符串。

+0

嗨,如果我想要'名稱服務器'值,它沒有固定的長度,我怎麼得到它?可能嗎? – pleasega

+1

$ data = preg_match_all('/ Name Server:(。*)\ n /',$ str,$ match); @pleasega –