如何从纯文本响应中获取单个数据?

时间:2016-11-12 09:22:31

标签: php html regex api

如何从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 个答案:

答案 0 :(得分: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来匹配字符串。