使用php解析HTTP API响应


parsing http api response with php

我正在使用域名经销商API,它在html中提供响应。但是,它根本没有格式化。我得到了这样一个字符串:

domain.com: error: 102 your credentals are wrong 

other-domain.net: OK: 202 domain is available

我只对状态码感兴趣,通过它我可以向客户端发送自定义消息。如何从这个字符串中解析代码?请注意,响应的长度可以变化。我正在考虑使用reg exp ?

谢谢

这段代码可能行得通

$string='other-domain.net: OK: 202 domain is available';
$array = explode(' ', $string);
echo $array[2]; // 202

状态码显然总是以冒号和空格作为前缀

$statusText = 'other-domain.net: OK: 202 domain is available';
$status = preg_replace( '/.*: ('d+).+/', '$1', $statusText );